public static void main(String[] args) {
//声明Connection对象
Connection con;
//驱动程序名
String driver = "com.mysql.cj.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=GMT&useSSL=false";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "123456";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url, user, password);
if (!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.创建statement类对象,用来执行SQL语句!!
String sql = "insert into account(username,name,age) values(?,?,?)";
PreparedStatement preparedStatement = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
//start widh 1
preparedStatement.setString(1,"6");
preparedStatement.setString(2,"6");
preparedStatement.setString(3,"6");
preparedStatement.executeUpdate();
ResultSet rs = preparedStatement.getGeneratedKeys();
rs.next();
int id = rs.getInt(1);
System.out.println("id="+id);
} catch (Exception e) {
e.printStackTrace();
}
}
jdbc获取自增主键ID
原创
©著作权归作者所有:来自51CTO博客作者kq1983的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:liunx 别名 alias
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
MyBatis获取自增ID
MyBatis执行insert操作获取自增ID
MyBatis MySql