jdbc以事物方式往mysql写数据

来源:互联网 发布:阿莱克丝塔萨 知乎 编辑:程序博客网 时间:2024/05/21 00:01
package com.alibaba.fyq;


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


public class JDBCTest {
public static void main(String[] args){
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://10.20.153.185:3306/fengyaqing";
String user = "fengyaqing";
String pwd = "fengyaqing";
Connection connection = DriverManager.getConnection(url, user, pwd);
if(!connection.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = connection.createStatement();
connection.setAutoCommit(false);
statement.executeUpdate("insert into student values (02011001,'钏李四','男')");
statement.executeUpdate("insert into student values (02011002,'立场','男')");
statement.executeUpdate("insert into student values (02011003,'钏李四','男')");
statement.executeUpdate("insert into student values (02011004,'钏李四','男')");
statement.executeUpdate("insert into student values (02011005,'钏李四','男')");
statement.executeUpdate("insert into student values (02011006,'钏李四','男')");
statement.executeUpdate("insert into student values (02011007,'钏李四','男')");
statement.executeUpdate("insert into student values (02011008,'钏李四','男')");
statement.executeUpdate("insert into student values (02011009,'钏李四','男')");
statement.executeUpdate("insert into student values (02011010,'钏李四','男')");
connection.commit();
connection.setAutoCommit(true);
statement.close();
connection.close();
}catch (ClassNotFoundException e) {
// TODO: handle exception
System.out.println("Sorry,can't find the Driver!");
e.printStackTrace();
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

}
}
复制搜索