Java 自动提交假表的最大值

Java 自动提交假表的最大值,java,mysql,jsp,commit,rollback,Java,Mysql,Jsp,Commit,Rollback,我的问题是我已将表自动提交设置为false。我需要从该表中获取max id(当前插入的auto increment id值)。但我正在获取上一个已提交进程的id。有可能得到这个值吗 我真正的问题是我需要在表中插入一些值,并且需要从第一个表中获取最后一条插入记录的id并将其插入到第二个表中。第二次插入还包括一些图像上传(以及部分代码)。所以它需要一些延迟,或者可能有例外。我需要通过发生任何异常来撤消所有插入(第一次和第二次)。为此,我尝试使用提交回滚方法。但是它没有像我上面提到的那样正常工作。下面

我的问题是我已将表自动提交设置为false。我需要从该表中获取max id(当前插入的auto increment id值)。但我正在获取上一个已提交进程的id。有可能得到这个值吗

我真正的问题是我需要在表中插入一些值,并且需要从第一个表中获取最后一条插入记录的id并将其插入到第二个表中。第二次插入还包括一些图像上传(以及部分代码)。所以它需要一些延迟,或者可能有例外。我需要通过发生任何异常来撤消所有插入(第一次和第二次)。为此,我尝试使用提交回滚方法。但是它没有像我上面提到的那样正常工作。下面是我代码的主要部分

try
{

//getting connection and setting auto commit false
dbHandler dbGetConnect=new dbHandler();
Connection conRegPlot=null;
conRegPlot=dbGetConnect.getconn();
conRegPlot.setAutoCommit(false);


String qryInsertPlot="INSERT INTO property_table(name,message,owner,locality,lattitude,longitude,country,state,city,type,catagory,created,modified,creted_date,zoompoint,mob_no,title,img_path,expire_date,lease_term) VALUES('none','"+description+"','"+sessionUserId+"','"+locality+"','"+lattitude+"','"+longitude+"','"+country+"','"+state+"','"+city+"','"+type+"','"+catagory+"',NOW(),NOW(),CURDATE(),'"+zoom+"','"+mob_no+"','"+title+"','NOT IN USE',"+expireDate+",'"+termsAndConditions+"')";//insertion into the first table

Statement stCrs=conRegPlot.createStatement();
int resp=stCrs.executeUpdate(qryInsertPlot);

String qryGetMaxProperty="SELECT MAX(l_id)"+
        " FROM property_table"+
            " WHERE stat='active'"+
            " AND CURDATE()<=expire_date";
propertyId1=dbInsertPropert.returnSingleValue(qryGetMaxProperty);// get the max id


String qryInsertImage="INSERT INTO image_table(plot_id,ownr_id,created_date,created,modified,stat,img_path) VALUES('"+propertyId1+"','"+sessionUserId+"',CURDATE(),NOW(),NOW(),'active','"+img_pth+"')";

            Statement stImg=conRegPlot.createStatement();
            stImg.executeUpdate(qryInsertImage);// inserting the image


conRegPlot.commit();    
}
catch(Exception exc)
{
    conRegPlot.rollback();

}
finally{
    conRegPlot.close();
}
试试看
{
//获取连接并将自动提交设置为false
dbHandler dbGetConnect=new dbHandler();
连接conRegPlot=null;
conRegPlot=dbGetConnect.getconn();
conRegPlot.setAutoCommit(假);
String qryInsertPlot=“插入到属性”表(名称、消息、所有者、位置、晶格度、经度、国家、州、城市、类型、分类、创建、修改、认证日期、缩放点、移动编号、标题、img路径、过期日期、租约期限)值('none'、'+description+'、'+sessionUserId+'、'+locality+'、'+locality+'、'+latitude+'、'+longitude+'、“+country+”、“+state+”、“+city')+“、”、“+type+”、“+catagory+”、NOW()、NOW()、CURDATE()、“+zoom+”、“+mob_no+”、“+title+”、“+expireDate+”、“+termsAndConditions+”);//插入到第一个表中
语句stCrs=conRegPlot.createStatement();
int resp=标准执行更新(qryInsertPlot);
String qryGetMaxProperty=“选择最大值(l\U id)”+
“从属性表”+
“WHERE stat='active'”+
“和CURDATE()自

并且需要从第一条记录中获取最后一条插入记录的id 表并将其插入到第二个

您可以使用新的
JDBC 3.0
方法
getGeneratedKeys()
来获取生成的ID。另一方面,您应该使用
PreparedStatement
来避免SQL注入

 PreparedStatement ps = null;
 try {
    conn = getConnection();
    ps = conn.prepareStatement(myQuery, PreparedStatement.RETURN_GENERATED_KEYS);
    ps.setInt(1, anInteger);
    ...
    int rows = ps.executeUpdate();
    if (rows == 1) {
        ResultSet keysRS = ps.getGeneratedKeys();
        if (keysRS.next())
            int id = keysRS.getInt(1) // Get generated id

对于MySQL,请参阅

中的更多内容。该网站应称为SQL注入,而不是堆栈溢出。有人曾经逃避过他们在SQL语句中添加的内容吗?非常感谢你的回答,它对我很有效。我将尝试使用预处理语句