jdbc connection problems and routine operations

The first question: jdbc driver for mysql registered in two ways The first way is to use reflection to load the driver,

This is com.mysql.jdbc.Driver () of source code, when you use this reflection to load Driver class, a static block will be loaded! This will complete the driver loaded the mysql,

The second is, without reflection, you load a new database-driven way, because this is a Driver implementation class, but the way you new object has drawbacks, first you had a hard-coded Why do I say!

 

You might say that you first approach it is not hard-code the code, if I use a properties file with key-value pairs stored "com mysql.jdbc.Driver" stored, if I use the oracle database, I modified key-value pairs on the line,

The second problem is that you load two new Driver () This class is loaded twice driver, why do you say? You want your new com.mysql.jdbc.Driver () of this class, just load the driver, this is a; the dynamic drive method of the first load mysql above, which static code block is also loaded, because static code block when the new object will be executed first, static code block also been carried out, which the driver will be loaded!

In pull it free articles, if you thought about the way new since the object was loaded twice, then the static code block is removed, that if you get rid of that first way a reflection of the way it not useless!

Posted about the way the first two ways of connecting to the database

The first, using the reflection

Class.forName("com.mysql.jdbc.Driver");    //利用反射加载mysql数据库驱动
        Connection conn = null;
        PreparedStatement st = null;
        try {
            //获取数据库连接对象
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day101","root","123456");
            conn.setAutoCommit(false);//设置为手动提交sql语句,jdbc默认是自动提交,可以手动提交,但是自动提交不会有事务回滚哦!会出现脏读等现象
            String sql = "update account set name=? where id=?"; 
            st = conn.prepareStatement(sql);//获取预编译对象并且预编译sql,
            //关于PreparedStatement和Statement的区别我在下一篇博客讲解
            st.setObject(1,"5");//设置占位符jdbc的占位符索引从1开始
            st.setObject(2, 1);
             int i = st.executeUpdate();//如果i为1的话表示执行更新语句没毛病
            conn.commit();//提交更新操作
        } catch (SQLException e) {
            try {
                conn.rollback();//如果发生异常就执行回滚,防止发生脏读等不友好的操作,
                //关于脏读和其他的一些数据库不友好的操作我在写一篇博客
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            try {
                /**
                 * 下面的操作是执行关闭操作,步骤为从下往上关闭
                 * 这里解释一下,如果你先关闭Connection这个对象的话,那么会导致后面的
                 * PreparedStatement对象无效(注意这里只是无效,并没有释放物理连接哦)
                 */
                st.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

 第二种方式 :利用new对象的方式进行数据库连接

Connection conn = null;
		PreparedStatement st = null;
		try {
			//加载数据库驱动
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			//获取数据库连接对象
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day101","root","123456");
			conn.setAutoCommit(false);//设置为手动提交sql语句,jdbc默认是自动提交,可以手动提交
			String sql = "update account set name=? where id=?"; 
			st = conn.prepareStatement(sql);//获取预编译对象并且预编译sql,
			//关于PreparedStatement和Statement的区别我在下一篇博客讲解
			st.setObject(1,"5968");//设置占位符jdbc的占位符索引从1开始
			st.setObject(2, 1);
			int i = st.executeUpdate();//如果i为1的话表示执行更新语句没毛病
			conn.commit();
		} catch (SQLException e) {
			
		}finally{
			try {
				/**
				 * 下面的操作是执行关闭操作,步骤为从下往上关闭
				 * 这里解释一下,如果你先关闭Connection这个对象的话,那么会导致后面的
				 * PreparedStatement对象无效(注意这里只是无效,并没有释放物理连接哦)
				 */
				 if(st!=null){
					 st.close();
				 }
				if(conn!=null){
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

  上面的两种方式你都要导入mysql的jar

Guess you like

Origin www.cnblogs.com/tranquilityMan/p/10990214.html