JAVA中三种获得⾃动⽣成主键的⽅法
package test;
turnaround
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 三种获得⾃动⽣成主键的⽅法。
会计证怎么考取需要什么条件
*
*/
public class TestGetPK
{
public static void main(String[] args) throws Exception
{
Class.forName("com.gba.jdbc.Driver" );
String url = "jdbc:gba://localhost/mytest" ;
Connection con = Connection(url, "root" , "111111" );
System.out.println(getPK1(con));
System.out.println(getPK2(con));
System.out.println(getPK3(con));
}
/**
曲阜师范大学分数线
* 使⽤JDBC 3.0提供的 getGeneratedKeys。推荐使⽤
*
* @param con
* @return
* @throws Exception
*/
public static long getPK1(Connection con) throws Exception
{
lsp是什么意思Statement stmt = ateStatement();
Statement.RETURN_GENERATED_KEYS);
int autoIncKeyFromApi = -1 ;
ResultSet rs = GeneratedKeys();
if (rs.next())
会计核算
{
autoIncKeyFromApi = rs.getInt(1);
}
return autoIncKeyFromApi;
白色的英文
}
/**
* 使⽤数据库⾃⼰的特殊SQL.
*
* @param con
assignable* @return
* @throws Exception
*/
public static long getPK2(Connection con) throws Exception
{
Statement stmt = ateStatement();
Statement.RETURN_GENERATED_KEYS);
int autoIncKeyFromFunc = -1 ;
ResultSet rs = uteQuery( "SELECT LAST_INSERT_ID()");
if (rs.next())
{
autoIncKeyFromFunc = rs.getInt(1);
}
}
return autoIncKeyFromFunc;
}
/**
* 使⽤可更新的结果集。
* @param con
* @return
during
* @throws Exception
*/
public static long getPK3(Connection con) throws Exception
{
fulfilled
Statement stmt = ateStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE);
ResultSet rs = uteQuery("SELECT * FROM t_Type");
rs.updateString("name", "AUTO INCREMENT here?");
rs.inrtRow();
rs.last();
int autoIncKeyFromRS = rs.getInt("id" );
return autoIncKeyFromRS;
美国voa慢速英语}
}