JDBC例子——实现增删改

package crud;

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

import org.junit.Test;

/**
 * 这个类用来完成jdbc增删改查业务
 */
public class JDBCCRUD {
    //向user表中插入一条记录
    @Test
    public void add(){
        Connection conn=null;
        Statement st = null;
        int rows=0;
        //1,注册驱动 2,获取连接3,获取传输器4,执行sql5,遍历结果集6,释放资源
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql:///st_db";
            String userName="root";
            String passWord="123456";
            conn = DriverManager.getConnection(url, userName, passWord);
            st=conn.createStatement();
            String sql="insert into user values(null,'apple','123456')";
            rows=st.executeUpdate(sql);
            System.out.println(rows+" rows affected");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(st!=null)
            {//防止空指针异常
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    st=null;//手动置空
                }
            }
            if(conn!=null)
            {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    conn=null;//手动置空
                }
            }
        }
    }
    //修改account表中id为1的记录,money为400
    @Test
    public void update(){
        Connection conn=null;
        Statement st=null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取数据库连接
            String url="jdbc:mysql:///st_db";
            String userName="root";
            String passWord="123456";
            conn=DriverManager.getConnection(url,userName,passWord);
            st=conn.createStatement();
            String sql="update account set money=400 where id=1";
            int rows=st.executeUpdate(sql);
            System.out.println(rows+" rows affected");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            //释放资源
            if(st!=null){
                //防止空指针
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    st=null;
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    conn=null;
                }
            }
        }
    }
    
    
    //删除user表里id为4的记录
    @Test
    public void delete(){
        Connection conn=null;
        Statement st=null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql:///st_db";
            String userName="root";
            String passWord="123456";
            //获取连接
            conn=DriverManager.getConnection(url,userName,passWord);
            //获取传输器
            st=conn.createStatement();
            //执行sql
            String sql="delete from user where id=4";
            int rows=st.executeUpdate(sql);
            //遍历结果集 
            System.out.println(rows+" rows affected");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            //释放资源
            if(st!=null)
            {
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    st=null;
                }
            }
            if(conn!=null)
            {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    conn=null;
                }
            }
        }
    }
}

你可能感兴趣的:(JDBC例子——实现增删改)