MySQL_(Java)提取工具類JDBCUtils

 

 

  MySQL_(Java)使用JDBC向數據庫發起查詢請求  傳送門html

  MySQL_(Java)使用JDBC建立用戶名和密碼校驗查詢方法  傳送門java

  MySQL_(Java)使用preparestatement解決SQL注入的問題  傳送門mysql

 

  使用工具類JDBCUtils意義:在作增、刪除、修改、查詢都須要獲取Connection鏈接,使用完畢以後咱們都須要關閉鏈接,這些工做是不斷的重複在作的事情,因此咱們能夠把這些工做定義成一個工具類的方法,減小咱們重複代碼的編寫sql

  

  MySQL數據庫中的數據,數據庫名garysql,表名garytb數據庫

  

 

  經過JDBC對MySQL中的數據進行查詢ide

 

 

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

public class JDBC01 {

    public static void main(String[] args) throws SQLException  {
        selectAll();
    }

    public static void selectAll() throws SQLException {
        //註冊驅動    使用驅動鏈接數據庫
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            
            //數據庫的增刪改查
            stmt = con.createStatement();
            //返回一個結果集
            rs =stmt.executeQuery("select * from garytb");
            
            while(rs.next()) {
                //System.out.println(rs.getString(1)+","+rs.getString(2)+","+rs.getString(3));
                System.out.println(rs.getString("id")+","+rs.getString("username")+","+rs.getString("password"));
            }
        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs, stmt, con);
        }
    }

    public static boolean  selectByUernamePassword(String username,String password) throws SQLException {
        Connection con=null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            
            String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
            con = DriverManager.getConnection(url,"root","123456");
            stmt =con.createStatement();
            String sql = "select * from garytb where username = '"+username+"' and password = '"+password+"'";
            //System.out.println(sql);
            rs = stmt.executeQuery(sql);
            
            if(rs.next()) {
                return true;
            }else {
                return false;
            }
                
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(con!=null)
                con.close();
        }
        
        return false;
    }

    public static boolean selectByUP2(String username,String password) throws SQLException{
        Connection con=null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            
            String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
            con = DriverManager.getConnection(url,"root","123456");
            
            String sql = "select * from garytb where username = ? and password = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            //添加參數
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            //進行查詢
            rs = pstmt.executeQuery();
                
            if(rs.next()) {
                return true;
            }else {
                return false;
            }
                
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(con!=null)
                con.close();
        }
        
        return false;
    }

    //pageNumber是頁數,第幾頁,pageCount是每頁顯示多少個數據
    public static void selectUserByPage(int pageNumber,int pageCount) throws SQLException {
        //註冊驅動    使用驅動鏈接數據庫
                Connection con = null;
                PreparedStatement stmt = null;
                ResultSet rs = null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    
                    //String url ="jdbc:mysql://localhost:3306/garysql";
                    //指定編碼查詢數據庫
                    String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
                    String user = "root";
                    String password = "123456";
                    //創建和數據庫的鏈接
                    con = DriverManager.getConnection(url,user,password);
                    
                    stmt = con.prepareStatement("select * from garytb limit ?,?");
                    stmt.setInt(1, (pageNumber-1)*pageCount );
                    stmt.setInt(2, pageCount);
                    
                    rs = stmt.executeQuery();
                    
                    while(rs.next()) {
                        //System.out.println(rs.getString(1)+","+rs.getString(2)+","+rs.getString(3));
                        System.out.println(rs.getString("id")+","+rs.getString("username")+","+rs.getString("password"));
                    }
                
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally {
                    if(rs!=null)
                        rs.close();
                    if(stmt!=null)
                        stmt.close();
                    if(con!=null)
                        con.close();
                }
    }
}
JDBC01.java

 

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

public class JDBCUtils {
    
    private static final String connectionURL = "jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
    private static final String username = "root";
    private static final String password = "123456";
    
    //建立數據庫的鏈接
    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            return   DriverManager.getConnection(connectionURL,username,password);
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        return null;
    }
    
    //關閉數據庫的鏈接
    public static void close(ResultSet rs,Statement stmt,Connection con) throws SQLException {
        if(rs!=null)
            rs.close();
        if(stmt!=null)
            stmt.close();
        if(con!=null)
            con.close();
    }
}
JDBCUtils.java

 

   在JDBCUtils.java中實現構建對數據庫的鏈接和關閉數據庫操做工具

private static final String connectionURL = "jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
    private static final String username = "root";
    private static final String password = "123456";
    
    //建立數據庫的鏈接
    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            return   DriverManager.getConnection(connectionURL,username,password);
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        return null;
    }
    
    //關閉數據庫的鏈接
    public static void close(ResultSet rs,Statement stmt,Connection con) throws SQLException {
        if(rs!=null)
            rs.close();
        if(stmt!=null)
            stmt.close();
        if(con!=null)
            con.close();
    }

 

  在JDBC01.java中須要對數據庫中用戶查詢時直接調用JDBCUtils.java中方法便可編碼

     //數據庫的鏈接
    con = JDBCUtils.getConnection(); 

    //關閉數據庫
    JDBCUtils.close(rs, stmt, con);               

 

  例如查詢MySQL中全部用戶的信息url

public static void selectAll() throws SQLException {
        //註冊驅動    使用驅動鏈接數據庫
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //數據庫的鏈接
            con = JDBCUtils.getConnection();
            
            //數據庫的增刪改查
            stmt = con.createStatement();
            //返回一個結果集
            rs =stmt.executeQuery("select * from garytb");
            
            while(rs.next()) {
                //System.out.println(rs.getString(1)+","+rs.getString(2)+","+rs.getString(3));
                System.out.println(rs.getString("id")+","+rs.getString("username")+","+rs.getString("password"));
            }
        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs, stmt, con);
        }
    }
相關文章
相關標籤/搜索