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

Posted fbwa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
    }
}

 

以上是关于JDBC操作数据库--增删改查的主要内容,如果未能解决你的问题,请参考以下文章

MYBATIS01_概述及优缺点快速搭建工程Mybatis的增删改查操作总结

JDBC实现Mysqual的增删改查

java连接mysql以及增删改查操作

DBUtils (增删改查)

JDBC_增删改查

JDBC之增删改查的代码实现