dbconfig.properties文件
#MySQL
drivername = com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jeewx?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
#Oracle
#drivername = oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:ORCL
#username=username
#password=password
//连接数据库工具类
package gacl.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
@SuppressWarnings("unused")
public class DBConnection {
//方法1:
// private static final String URL = "jdbc:mysql://localhost:3306/jeewx?useUnicode=true&characterEncoding=UTF-8";
// private static final String DRIVERNAME = "com.mysql.jdbc.Driver";
// private static final String USER = "root";
// private static final String PASSWORD = "123456";
// public static Connection getConnection() throws SQLException{
// Connection conn = null;
// try {
// Class.forName(DRIVERNAME);
// conn = DriverManager.getConnection(URL,USER,PASSWORD);
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
//
// return conn;
//
// }
//方法2:通过配置文件获取数据库信息(推荐使用)
private static String url = null;
private static String driverName = null;
private static String user = null;
private static String password = null;
static{
System.out.println("=========================");
Properties props = getProps();
driverName = props.getProperty("drivername");
url = props.getProperty("url");
user = props.getProperty("username");
password = props.getProperty("password");
}
public static Connection getConnection() throws SQLException{
Connection conn = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection(ResultSet rs,Statement stmt,Connection conn){
//关闭数据库
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch(Exception e) {
System.out.println("数据库关闭错误");
e.printStackTrace();
}
}
public static Properties getProps(){
Properties props = new Properties();
try {
InputStream in = DBConnection.class.getResourceAsStream("dbconfig.properties");
// 或使用文件输入流(不推荐),假设当前工作目录为bin
//InputStream in = new FileInputStream("layer.properties");
props.load(in);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return props;
}
}
//测试类
package gacl.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* ==============================================
* <p>TODO JDBC测试类</p>
* <p>Copyright: 版权所有 (c) 2002 - 2008<br>
* Company: 久其</p>
* @author xuhaiyang
* @version 2016-8-13
* ==============================================
*/
@SuppressWarnings("unused")
public class TestJdbc {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = null;
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
sql = "select * from t_s_student";
rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.print(rs.getString("classname") + " ");
System.out.println(rs.getString("name") + " ");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnection.closeConnection(rs, stmt, conn);
}
}
}