2014年5月22日星期四

JDBC transaction

today to learn JDBC transaction processing , I do not set auto-commit and rollback when wrong, but why will still be executed after running the update operation does no wrong
direct look at the code it
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BatchDemo01{
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/winestore" ;
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "root" ;

public static void main(String[] args) throws Exception{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);

conn.setAutoCommit(false);//取消自动提交

st=conn.createStatement();
try{
st.addBatch("insert into users(user_name,password) values('剑南春','jiannanchun')");
st.addBatch("insert into users(user_name,password) values('口子窖','kouzijiao')");
//下面的sql语句中我把user_name错误的写成username,
st.addBatch("insert into users(username,password) values('茅台','maotai')");
st.addBatch("insert into users(user_name,password) values('汾酒','fenjiu')");
int[] len=st.executeBatch();
System.out.println("更新了"+len.length+"条数据");
conn.commit();//提交事务
}catch(Exception e){
conn.rollback();//如果出错,则回滚
}
conn.close();//关闭连接
}
}

When I perform discovery : In addition to the wrong piece of SQL is not executed , and the rest were executed. Is my understanding wrong? Members seeking doubts
------ Solution - -------------------------------------------
landlord 's code did not problem , you check under your mysql driver, and you use mysql database engine (preferably using innoDB engine ) .
------ For reference only -------------------------------------- -
st not closed ah ,
------ For reference only ---------------------------- -----------
should be something you have a problem
------ For reference only ----------------- ----------------------
seemingly is not closed java st try to learn something in a long time had not What might have cleared
------ For reference only --------------------------------- ------

Oh, shut up or useless
------ For reference only ---------------------------------------

//判断数据库是否支持事务处理
DatabaseMetaData md=conn.getMetaData();
if(md.supportsTransactions()){
System.out.println("数据库支持事务处理");
}

I was judging my Mysql database supports transaction processing , the results display support
------ For reference only ---------------------------------------


/ / determine whether to support transaction processing database
DatabaseMetaData md = conn.getMetaData ();
if (md.supportsTransactions ()) {
System.out.println (" database supports transaction processing " ) ;
}
I was judging my Mysql database supports transaction processing , the results display support
------ For reference only ---------------------------------------

landlord that these whether the data is rolled back ?
------ For reference only -------------------------------------- -

that the landlord whether these data are rolled back ?  

I just open my database , checked the users table , discovery engine using MyIsam ..........
get it , thank you

没有评论:

发表评论