java连接mysql数据库

1 编写工具类JDBCUtils

  使用PreparedStatement接口解决mysql注入的问题

public class JDBCUtils {

    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/day04";
    private static String user = "root";
    private static String password = "root";
    static{
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException();
        } 
    }
    
    public static Connection getConnection() throws SQLException{
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
        
    }
    
    public static void closeResource(Connection conn, Statement st, ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(st != null){
            try {
                st.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } 
}

2 使用工具类操作数据库

public class AddDemo {
    
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    PreparedStatement psmt = null;
    
    @Test
    public void demo5(){
        try {
            conn = JDBCUtils2.getConnection();
            String sql = "insert into category (cname) values (?)";
            psmt = conn.prepareStatement(sql);
            psmt.setString(1, "预处理");
            int r = psmt.executeUpdate();
            System.out.println(r);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException();
        }
    }
    
    @Test
    public void demo6(){
        try {
            conn = JDBCUtils2.getConnection();
            String sql = "update category set cname = ? where cid = ?";
            psmt = conn.prepareStatement(sql);
            psmt.setString(1, "测试数据");
            psmt.setInt(2, 4);
            
            int r = psmt.executeUpdate();
            System.out.println(r);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException();
        }
        finally {
            JDBCUtils2.closeResource(conn, psmt, rs);
        }
        
    }
    
    @Test
    public void demo7() {
        try {
            conn = JDBCUtils2.getConnection();
            String sql = "select * from category where cid = ?";
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1, 2);
            rs = psmt.executeQuery();
            if(rs.next()){
                System.out.println("查询到");
            }else {
                System.out.println("查询不到");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException();
        }finally {
            JDBCUtils2.closeResource(conn, psmt, rs);
        }
    }
}

 

posted on 2019-07-02 09:44  backend  阅读(513)  评论(0编辑  收藏  举报

导航