jdbc操作AutoCommit

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class TestAutoCommit {


public static void main(String[] args) throws Exception {
TestAutoCommit auto=new TestAutoCommit();
Connection conn=auto.getConn();
boolean commit=conn.getAutoCommit();
System.out.println("自动提交:  "+commit);

//关闭提交功能
conn.setAutoCommit(false);
System.out.println("自动提交: "+conn.getAutoCommit());
Statement stmt=conn.createStatement();
stmt.execute("insert into student(name,sex,grade) values('xxxxx','m','2008811237')");
stmt.execute("insert into student(name,sex,grade) values('yyyyy','f','2008811219')");
ResultSet rs=stmt.executeQuery("select * from student");

while(rs.next()){
System.out.println("Row: "+rs.getRow()+"\tId: "+rs.getInt(1)+"\tName: "+rs.getString(2)
+"\tSex: "+rs.getString(3)+"\tGrade: "+rs.getString(4));
}
// 提交事务
conn.commit();
System.out.println("-------------------------------------");
rs=stmt.executeQuery("select * from student");
while(rs.next()){
System.out.println("Row: "+rs.getRow()+"\tId: "+rs.getInt(1)+"\tName: "+rs.getString(2)
+"\tSex: "+rs.getString(3)+"\tGrade: "+rs.getString(4));
}
//恢复原来的提交模式
conn.setAutoCommit(commit);
stmt.close();
}

public Connection getConn() {
Connection conn=null;
String url="jdbc:mysql://localhost/dandan?user=root&password=dada";
try {
Class.forName("org.gjt.mm.mysql.Driver");
try {
conn=DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值