写个JDBC工具类怕以后忘记了:
public class JdbcUtil {
public static Connection getConnection() throws Exception {
// 1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
/*
Properties properties=new Properties();
properties.load(new FileInputStream("db.properties"));
//获取各种属性
String user=properties.getProperty("user");
String password=properties.getProperty("password");
String url=properties.getProperty("url");
String driverClass=properties.getProperty("driverClass");
*/
String user="root";
String password="123456";
String url="jdbc:mysql://localhost:3306/haosql";
Connection conn=DriverManager.getConnection(url, user, password);
return conn;
}
public static void releaseSourse(Connection conn,Statement stmt,ResultSet result) {
try {
conn.close();
stmt.close();
result.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void releaseSourse(Statement stmt,ResultSet result) {
try {
stmt.close();
result.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void releaseSourse(Connection conn,Statement stmt) {
try {
conn.close();
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void releaseSourse(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}