- 1 package xmlDom.dbc;
- 2
- 3 import java.sql.Connection;
- 4 import java.sql.DriverManager;
- 5 import java.sql.SQLException;
- 6
- 7 public class DatabaseConnection
- 8 {
- 9 private static finalString MYSQLDRIVER = "org.gjt.mm.mysql.Driver";
- 10 private static finalString MYSQLURL = "jdbc:mysql://localhost:3306/hrdb";
- 11 private static finalString USERNAME = "root";
- 12 private static finalString PASSWORD = "admin";
- 13
- 14 private Connection conn;
- 15
- 16 public DatabaseConnection()
- 17 {
- 18 try
- 19 {
- 20 Class.forName(MYSQLDRIVER);
- 21 this.conn = DriverManager.getConnection(MYSQLURL, USERNAME, PASSWORD);
- 22}catch (Exception e)
- 23 {
- 24 e.printStackTrace();
- 25 }
- 26 }
- 27
- 28 publicConnection getConnection()throws Exception
- 29 {
- 30 if(null!=this.conn)
- 31 return this.conn;
- 32 else
- 33 throw newException("获取数据库连接失败");
- 34 }
- 35
- 36 public void close()
- 37 {
- 38 if(null!=this.conn)
- 39 {
- 40 try
- 41 {
- 42 this.conn.close();
- 43}catch (SQLException e)
- 44 {
- 45 e.printStackTrace();
- 46 }
- 47 }
- 48 }
- 49}
来源: