JDBC增删改查

JDBC增删改查

1.案例需求:jdbc增删改查
2.需要注意的:a.优化了注册驱动的步骤:

//1.注册驱动com.mysql.jdbc.Driver  
DriverManager.registerDriver(new Driver());

缺点:1.造成驱动被注册了两次
   2.是程序和驱动产生了紧耦合
解决方法:Class.forName(“com.mysql.jdbc.Driver”); 
 
      b.传输器对象Statement
      作用:用来执行SQL
      提供的常用方法:
      executeQuery —执行查询的SQL
      executeUpdate — 执行增删改的SQL,返回int 类型

package on.jdbc;

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

import org.junit.Test;

/**
 * 这个类用做jdbc的增删改查操作
 * 
 * @date 2018年3月23日
 *
 */
public class JDBCCURD {
    Connection conn=null;
    Statement st=null;
    //需求:向account中插入一条记录
    //name为xiongda,money为2000
    @Test
    public void add(){
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取数据库连接java.sql.Connection
            conn=DriverManager.getConnection("jdbc:mysql:///jt_db","root","root");
            //3.获取传输器java.sql.Statement
            st=conn.createStatement();
            String sql="insert into account values( null,'xiongda','2000')";
            //4.执行SQL语句
            int rows=st.executeUpdate(sql);//返回一个int
            //5.获取结果集
            System.out.println(rows);
        } catch (Exception e) {

            e.printStackTrace();
        }finally{
            //6.关闭资源
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    conn=null;
                }
            }
            if(st!=null){
                try {
                    st.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    //需求:修改account表中
    //id为3的money为1000
    @Test
    public void update(){
        Connection conn=null;
        Statement st=null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取数据库连接
            conn=DriverManager.getConnection("jdbc:mysql:///jt_db","root","root");
            //3.获取传输器
            st = conn.createStatement();//快捷键 Ctrl+1;
            //4.执行SQL
            String sql="update account set money=1000 where id=3";
            int rows=st.executeUpdate(sql);//返回一个int
            //5.获取查询结果集
            System.out.println(rows);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //6.关闭资源
            if(conn!=null){
                try{
                    conn.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }finally{
                    conn=null;
                }
            }
            if(st!=null){
                try{
                    st.close();
                }catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    //需求:
    //删除account表中id为3的数据
    @Test
    public void delete(){
        Connection conn=null;
        Statement st=null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取数据库的连接
            conn=DriverManager.getConnection("jdbc:mysql:///jt_db","root","root");
            //3.获取传输器
            st = conn.createStatement();    
            String sql="delete from account where id=3";
            //4.执行SQL
            int rows=st.executeUpdate(sql);
            //5.获取查询结果集
            System.out.println(rows);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //6.关闭资源
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    conn=null;
                }
            }
            if(st!=null){
                try {
                    st.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    st=null;
                }
            }
        }
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章