JAVA使用JDBC串連MySQL資料庫(3)-代碼部分

來源:互聯網
上載者:User

標籤:check   div   this   use   character   cti   false   string   cal   

 1     /**************串連資料庫部分********************/ 2     public static Connection conn(){ 3         Connection conn = null; 4         String driver = "com.mysql.jdbc.Driver"; 5         String url = "jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8"; 6         String username="root"; 7         String pw = "111111"; 8          9         try {10             Class.forName(driver);//指定連線類型 11             System.out.println("成功載入驅動程式!!!!");12             conn = (Connection) DriverManager.getConnection(url,username,pw);//擷取串連13         } catch (Exception e) {14             // TODO: handle exception15             e.printStackTrace();16         }17         return conn;18     }
 1     /**************操作部分********************/ 2     //管理員登入 3     public boolean checkAdmin(Admin admin){ 4         Connection conn = conn(); 5         String sql = "select * from user where username = ? and userpass= ? ";//SQL語句  6         PreparedStatement pstmt = null; 7         ResultSet rs = null; 8         boolean bl = false; 9         try{10             pstmt = (PreparedStatement) conn.prepareStatement(sql);11             pstmt.setString(1, admin.getAdminname()); //問號112             pstmt.setString(2,admin.getAdminpassword()); //問號213             rs = pstmt.executeQuery();  //結果集14             if(rs.next()){15                 System.out.println("登入成功");16                 bl = true;17             }else{18                 System.out.println("使用者名稱或密碼錯誤");                    19             }20             rs.close();21             pstmt.close();22             conn.close(); //關閉串連23         }catch(SQLException e){24             e.getStackTrace();25         }26         return bl;27     }
 1 package com.entity; 2  3 public class Admin { 4     private String adminname; 5     private String adminpassword; 6      7     public Admin(String adminname,String adminpassword){ 8         this.adminname = adminname; 9         this.adminpassword = adminpassword;10     }11     12     //管理員名字13     public String getAdminname() {14         return adminname;15     }16     public void setAdminname(String adminname) {17         this.adminname = adminname;18     }19     20     //管理員密碼21     public String getAdminpassword(){22         return adminpassword;23     }24     public void setAdminpassword(String adminpassword){25         this.adminpassword = adminpassword;26     }27 }
 1     /***********測試******************/ 2     public static void main(String[] args){ 3         Connsql sql = new Connsql(); 4         Admin admin = new Admin("amin","111111"); 5         boolean bl = sql.checkAdmin(admin); 6         if(bl){ 7             System.out.println("ok"); 8         }else{ 9             System.out.println("wrong");10         }11     }

 

JAVA使用JDBC串連MySQL資料庫(3)-代碼部分

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.