JDBC操作数据库--增删改查

JDBC操作增删改查

1、提取连接数据库代码

public class JDBCUtil {
    public static Connection getConnection(){
        Connection connection=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/kh87","root","200054");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  connection;
    }

    public static  void closeResoirce(PreparedStatement pstm,Connection connection){
        try {
            pstm.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static  void closeResoirce2(ResultSet rs, PreparedStatement pstm, Connection connection){
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            pstm.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConn(){
        Properties properties = new Properties();
        Connection connection=null;
        try {
            properties.load(cn.kgc.JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            String driverClass = properties.getProperty("driverClass");
            String url = properties.getProperty("url");
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            Class.forName(driverClass);
            connection = DriverManager.getConnection(url,username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;

    }

}

2、进行增删改查操作

public class test {
    Connection connection=null;
    PreparedStatement pstm=null;
    ResultSet rs=null;
    //增加数据
    public void insert(){
        connection=JDBCUtil.getConnection();
        String sql="insert into student values(?,?,?,?)";
        try {
            pstm=connection.prepareStatement(sql);
            pstm.setObject(1,4);
            pstm.setObject(2,"薛勇");
            pstm.setObject(3,"男");
            pstm.setObject(4,201);
            pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
           JDBCUtil.closeResoirce(pstm,connection);
        }

    }

    public void update(){
        connection=JDBCUtil.getConnection();
        String sql="update  student set sex=? where sid=?";
        try {
            pstm=connection.prepareStatement(sql);
            pstm.setObject(1,"男");
            pstm.setObject(2,"1");
            pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeResoirce(pstm,connection);
        }
    }

    public void delete(){
        connection=JDBCUtil.getConnection();
        String sql="delete from student where sid=?";
        try {
            pstm=connection.prepareStatement(sql);
            pstm.setObject(1,3);
            pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeResoirce(pstm,connection);
        }

    }
    public void search(){
        connection=JDBCUtil.getConnection();
        String sql="select * from student where sid=?";
        try {
            pstm=connection.prepareStatement(sql);
            pstm.setObject(1,"2");
            rs=pstm.executeQuery();
            if(rs.next()){
                int sid = rs.getInt("sid");
                String sname = rs.getString("sname");
                String sex = rs.getString("sex");
                int tid = rs.getInt("tid");
                System.out.println("sid:"+sid+",sname:"+sname+",sex"+sex+",tid"+tid);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeResoirce2(rs,pstm,connection);
        }
    }

    public void searchNo(){
        connection=JDBCUtil.getConnection();
        String sql="select * from student";
        try {
            pstm=connection.prepareStatement(sql);
            rs=pstm.executeQuery();
            while(rs.next()){
                int sid = rs.getInt("sid");
                String sname = rs.getString("sname");
                String sex = rs.getString("sex");
                int tid = rs.getInt("tid");
                System.out.println("sid:"+sid+",sname:"+sname+",sex"+sex+",tid"+tid);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeResoirce2(rs,pstm,connection);
        }

    }

    public static void main(String[] args) {
        test t=new test();
       // t.insert();
        t.searchNo();
    }
}

猜你喜欢

转载自www.cnblogs.com/fbwa/p/13195135.html