getGeneratedKeys自动获取主键的方法
2014-11-08 16:28
411 查看
public class Demo
{
public static void main(String[] args) {
try {
String sql="insert into person values(?,?)";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=pubs","sa","");
//需要增加一个Statement.RETURN_GENERATED_KEYS这样的参数
java.sql.PreparedStatement p=conn.PreparedStatement(sql,Statement.RETURN_GENERATED_KEYS);
p.setObject(1, "zengguofeef");
//p.setObject(2, new java.sql.Date(System.currentTimeMillis()));
//必须这样设置,System.currentTimeMillis()表示当前系统时间的毫秒数,Timestamp()方法会根据这个毫秒数设置成当前时间
p.setTimestamp(2, new java.sql.Timestamp(System.currentTimeMillis()));
p.executeUpdate();
//通过这个方法获取getGeneratedKeys();
ResultSet rs=p.getGeneratedKeys();
rs.next();
int num=rs.getInt(1);
System.out.println("自动增长列为:"+num);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
//在sql语句后面加上一个select scope_identity()
String sql="insert into person values(?,?) select scope_identity()";
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=pubs","sa","");
java.sql.PreparedStatement p=conn.prepareStatement(sql);
p.setObject(1, "zengguofeef");
p.setTimestamp(2, new java.sql.Timestamp(System.currentTimeMillis()));
//这里使用的是executeQuery()方法,而不是executeUpdate()
ResultSet rs=p.executeQuery();
rs.next();
int num=rs.getInt(1);
System.out.println("自动增长列为:"+num);
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
{
public static void main(String[] args) {
try {
String sql="insert into person values(?,?)";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=pubs","sa","");
//需要增加一个Statement.RETURN_GENERATED_KEYS这样的参数
java.sql.PreparedStatement p=conn.PreparedStatement(sql,Statement.RETURN_GENERATED_KEYS);
p.setObject(1, "zengguofeef");
//p.setObject(2, new java.sql.Date(System.currentTimeMillis()));
//必须这样设置,System.currentTimeMillis()表示当前系统时间的毫秒数,Timestamp()方法会根据这个毫秒数设置成当前时间
p.setTimestamp(2, new java.sql.Timestamp(System.currentTimeMillis()));
p.executeUpdate();
//通过这个方法获取getGeneratedKeys();
ResultSet rs=p.getGeneratedKeys();
rs.next();
int num=rs.getInt(1);
System.out.println("自动增长列为:"+num);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
//在sql语句后面加上一个select scope_identity()
String sql="insert into person values(?,?) select scope_identity()";
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=pubs","sa","");
java.sql.PreparedStatement p=conn.prepareStatement(sql);
p.setObject(1, "zengguofeef");
p.setTimestamp(2, new java.sql.Timestamp(System.currentTimeMillis()));
//这里使用的是executeQuery()方法,而不是executeUpdate()
ResultSet rs=p.executeQuery();
rs.next();
int num=rs.getInt(1);
System.out.println("自动增长列为:"+num);
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- getGeneratedKeys自动获取主键的方法
- java getGeneratedKeys(获取自动递增主键)
- 三种获得自动生成主键的方法,getGeneratedKeys,专用SQL和可更新的结果集
- 通过getGeneratedKeys获取记录的主键
- hibernate 中save之后怎么获取自增长的主键值? 类似servlet中的getGeneratedKeys();
- 通过getGeneratedKeys()获取主键
- java读取数据库指定表,生成bean,获取主键,自动生成get、set方法
- mysql获取自动生成的主键报错:Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.execu
- 关于jdbc保存后,返回自动增长的id,使用stmt.getGeneratedKeys();
- mysql数据库插入数据获取自增主键的三种方式(jdbc PreparedStatement方式、mybatis useGeneratedKeys方式、mybatis selectKey方式)
- 在JAVA中查询刚插入的记录ID 利用JDBC的getGeneratedKeys获得INSERT插入后生成的主键ID
- 批量插入getGeneratedKeys()方法使用
- Statement.RETURN_GENERATED_KEYS 获取自动增加的id号
- J2EE系列之Struts2学习笔记(二)---使用get/set方法自动获取/设置参数值
- oracle 获取插入 id getGeneratedKeys
- 在JAVA中查询刚插入的记录ID 利用JDBC的getGeneratedKeys获得INSERT插入后生成的主键ID
- jdbc-mysql基础 ResultSet getGeneratedKeys 插入一条字段并返回其主键
- winfrom 自动获取下一个控件GetNextControl方法
- 获取表单对象,得三种方法getElementById(), getElementsByName(), and getElementsByTagName() 和用法
- 使用HttpURLConnection开发自动发送Get、Post请求并获取响应