jdbc 返回主键值

package com.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
/**
* 数据库连接对象管理类
* @说明
* @author cuisuqiang
* @version 1.0
* @since
*/
public class ConnectionManager {
private static final String url = "jdbc:mysql://localhost:3306/test";
private static final String username = "root";
private static final String userpass = "root";
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception{
Connection conn = getConnection();
if (null != conn) {
String sql = "insert into common_user (name) values(?)";
// 指定返回生成的主键
PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
// 如果使用静态的SQL,则不需要动态插入参数
pstmt.setString(1, new Date().toLocaleString());
pstmt.executeUpdate();
// 检索由于执行此 Statement 对象而创建的所有自动生成的键
ResultSet rs = pstmt.getGeneratedKeys();
if (rs.next()) {
Long id = rs.getLong(1);
System.out.println("数据主键:" + id);
}
}
}
public static Connection getConnection() {
Connection conn = null;
try {
com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
Properties properties = new Properties();
properties.put("user", username);
properties.put("password", userpass);
conn = driver.connect(url, properties);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。