JDBC相关操作

#JDBC相关操作
##JDBC快速入门:

1.导入驱动jar包:1)复制文件;2)右键Add Library
	2.注册驱动
	class forName("com.mysql.jdbc.Driver");
	3.获取数据库连接对象
	String url = "jdbc:mysql//localhost:3306/db4";
	Connection conn = DriverManager.getConnection(url,"root","root");
	4.定义sql语句
	String sql = "update score set degree = 100 where sno = '123'";
5.获取执行sql的对象 Statement Statement stmt = conn.createStatement; 6.执行sql int count = stmt.executeUpdate(sql); 7.处理结果 System.out.println(count); 8.释放资源 stmt.close(); conn.close(); 注意事项:第2项可以省略不写,因为在我们导入MySQLjar包后,jar包下会有一个配置文件记录这个路径。

##JDBC完成数据的增删改查操作
###执行增删改操作(Update)

	...
	6.执行sql
	int count = stmt.executeUpdate(sql);
	...

###执行查询操作(Query)

	...
	6.执行sql
	ResultSet rs = stmt.executeQuery(sql);
	7.遍历结果集
	while(rs.next){
	int id = rs.getInt("id");
	String name = rs.getString("name");
	double money = rs.getDouble("money");
	//打印结果
	System.out.println(id + " " +name+ " " +money);
	}
	...
    释放资源顺序:先创建对象者,后释放;后创建对象者,先释放。

##PreperedStatement的用法和优点
###使用PreperedStatement的优点

JDBC相关操作_第1张图片

		*上图可以看到,用户名和密码都是不正确的,却可以通过sql注入方式登录成功,可见普通的Statement存在的安全问题很大。
	2.使用PreperedStatement的基本格式:
	...
	4.创建sql语句
	String sql = "select * from user where username = ? and password = ? ";
	5.创建PreperedStatement对象
	PreparedStatement pstmt = conn.prepareStatement(sql);
	6.给sql中的?赋值
	pstmt.setString(1,zhangsan);
	pstmt.setString(2,123);
	7.执行sql
	ResultSet rs = pstmt.executeQuery();
	...
3.使用prepareStatement的优点:
	*prepareStatement的执行效率更高。
	*prepareStatement的通过在sql语句中设置占位符,来防止sql注入,因此安全性更高。

##JDBC事务
#####JDBC概念
概念:JDBC通过数据库连接对象conn来管理事务,分别有如下三个操作。
*开启事务:conn.setAutoCommit(false);
*提交事务:conn.commit();
*回滚事务:conn.rollback();
三个步骤分别对应的位置:
1.开启事务:在conn对象创建之后
2.提交事务:在所有操作完成之后(一般在释放资源之前,执行完sql之后)
3.回滚事务:在遇到异常的时候(一般写在catch语句里面)
例如:

public class JDBCDemo10 {
			注*  JDBCUtils为自定义的JDBC工具类
		    public static void main(String[] args) {
		        Connection conn = null;
		        PreparedStatement pstmt1 = null;
		        PreparedStatement pstmt2 = null;
		
		        try {
		            //1.获取连接
		            conn = JDBCUtils.getConnection();
		            //开启事务
		            conn.setAutoCommit(false);
		
		            //2.定义sql
		            //2.1 张三 - 500
		            String sql1 = "update account set balance = balance - ? where id = ?";
		            //2.2 李四 + 500
		            String sql2 = "update account set balance = balance + ? where id = ?";
		            //3.获取执行sql对象
		            pstmt1 = conn.prepareStatement(sql1);
		            pstmt2 = conn.prepareStatement(sql2);
		            //4. 设置参数
		            pstmt1.setDouble(1,500);
		            pstmt1.setInt(2,1);
		
		            pstmt2.setDouble(1,500);
		            pstmt2.setInt(2,2);
		            //5.执行sql
		            pstmt1.executeUpdate();
		            // 手动制造异常
		            int i = 3/0;
		            	
		            pstmt2.executeUpdate();
		            //提交事务
		            conn.commit();
		        } catch (Exception e) {
		            //事务回滚
		            try {
		                if(conn != null) {
		                    conn.rollback();
		                }
		            } catch (SQLException e1) {
		                e1.printStackTrace();
		            }
		            e.printStackTrace();
		        }finally {
		            JDBCUtils.close(pstmt1,conn);
		            JDBCUtils.close(pstmt2,null);
		        }
		    }	
		}

你可能感兴趣的:(WEB核心技术)