title: JDBC笔记
date: 2017-02-24 22:20:15
tags: JDBC
JDBC笔记
- JDBC获取新插入数据自动生成的ID
public static <T> T executeInsert(String sql, ResultSetHandler<T> resultSetHandler,Object...param){
Connection connection = ConnectionManage.getConnection();
QueryRunner runner = new QueryRunner();
try {
return runner.insert(connection,sql,resultSetHandler,param);
} catch (SQLException e) {
e.printStackTrace();
}
finally {
close(connection);
}
return null;
}
//返回自动生成的键值
- ConnectionManager类
public class ConnectionMananger {
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:///RUNOOB","root","****");
return connection;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}