一、在使用jdbc对数据库中的数据进行操作时,首先需要在创建的工程中导入mysql-connector-java-5.0.8-bin jar包,然后在Eclipse中的具体操作步骤可以分为以下六步:
1、加载数据库驱动
2、获取链接
3、获取向数据库发sql语句的statement对象
4、向数据库发送sql,获取数据库返回的结果集
5、从结果集中获取数据
6、释放资源(释放链接)
在使用jdbc对数据库进行操作之前,对数据库进行的操作是:
Class.forName("com.mysql.jdbc.Driver"); //1.加载驱动程序;
String url=("jdbc:mysql://127.0.0.1:3306/usermanager"); //数据库连接串;
java.sql.Connection connection=null; //创建connection连接对象;
connection= DriverManager.getConnection(url, "root", "111111"); //2.获取数据库连接;
statement=connection.createStatement(); .在已创建的连接上新建一个statement对象,用来发送sql语句
(1)插入数据:
String sql1="insert into user values('an','31505610','1365928','[email protected]')"; //插入数据;
System.out.println("--->sql:" + sql1);
try {
statement.executeUpdate(sql1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// statement.executeUpdate(sql);
// statement.executeUpdate(sql);
// statement.executeUpdate(sql);
// statement.executeUpdate(sql);
//和上面语句的主要区别就在于 sql语句的多次执行和sql语句只执行一次
try {
statement.addBatch(sql1);
statement.addBatch(sql1);
statement.addBatch(sql1);
statement.addBatch(sql1);
statement.addBatch(sql1); //addBatch批处理sql语句;
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
statement.executeBatch(); //批提交数据到数据库;
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
(2)删除数据:
int ret=statement.executeUpdate("delete from user where name='zhang'"); //删除数据
System.out.println(ret); //ret返回的是所影响的行数;
(3)查询数据:
String sql2="select * from user;"; //查询数据;
ResultSet result=null;
try {
result=statement.executeQuery(sql2);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while(result.next()){
System.out.println("name:"+ result.getString(1) +"pwd:"+ result.getString(2) + "phone:"+ result.getString(3)
+ "email:"+ result.getString(4));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
result.close(); //释放资源(释放连接);
statement.close();
connection.close();
二、PreParedStatement
Class.forName("com.mysql.jdbc.Driver"); //1.加载驱动程序;
String url=("jdbc:mysql://127.0.0.1:3306/usermanager"); //数据库连接串;
java.sql.Connection connection=null; //创建connection连接对象;
connection= DriverManager.getConnection(url, "root", "111111"); //2.获取数据库连接;
//3.在已创建的连接上新建一个带sql预编译的statement对象,用来发送sql语句(防止常见的sql注入错误)
(1)插入数据
PreparedStatement prestatement=null;
String sql="insert into user(name,pwd,phone,email)values(?,?,?,?)";
try {
prestatement=connection.prepareStatement(sql);
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
prestatement.setString(1, "zhang");
prestatement.setString(2, "31505602");
prestatement.setString(3, "1365924");
prestatement.setString(4, "[email protected]");
prestatement.addBatch(); //利用addBatch批处理sql语句;
prestatement.addBatch();
prestatement.addBatch();
prestatement.addBatch();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
prestatement.executeBatch();
(2)查询数据
String sql2 = "select * from user;";
result = statement.executeQuery(sql2);
while(result.next()){
System.out.println("name:" + result.getString(1) +
" pwd:" + result.getString(2) +
" phone:" + result.getString(3) +
" email:" + result.getString(4));
}
三、手动提交事务:
事务指的是一系列的sql语句,要么全部执行成功,要么全部执行失败,不存在部分成功,部分失败的场景,因此,当所有sql语句执行成功的时候,事务会进行提交commit操作,把数据写到MYD文件中,如果有sql语句执行失败,那么事务会回滚rollback到最初没执行的状态, sql sql sql sql
**重点内容**默认情况下,jdbc的事务处理是自动提交的,它的粒度是每一条sql语句独自组成一个事务。
若要手动提交,则关闭自动提交;
connection.setAutoCommit(false);
connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); //设置隔离级别;允许脏读、不可重复读和幻读
sql语句发送后;
//手动提交事务的结果
connection.commit();