Java connects MySQL database with JDBC (3)-code section

Source: Internet
Author: User

1     /************** Connection Database section ********************/2      Public StaticConnection Conn () {3Connection conn =NULL;4String Driver = "Com.mysql.jdbc.Driver";5String url = "Jdbc:mysql://localhost:3306/demo?characterencoding=utf-8";6String username= "Root";7String pw = "111111";8         9         Try {TenClass.forName (driver);//Specify connection Type OneSYSTEM.OUT.PRINTLN ("Driver!!!! Loaded successfully"); Aconn = (Connection) drivermanager.getconnection (URL,USERNAME,PW);//Get Connections -}Catch(Exception e) { -             //Todo:handle Exception the e.printstacktrace (); -         } -         returnConn; -}
1     /************** Operation Section ********************/2     //Administrator Login3      Public Booleancheckadmin (admin admin) {4Connection conn =Conn ();5String sql = "SELECT * from user where username =?" and userpass=? ";//SQL statements6PreparedStatement pstmt =NULL;7ResultSet rs =NULL;8         BooleanBL =false;9         Try{TenPSTMT =(PreparedStatement) conn.preparestatement (SQL); OnePstmt.setstring (1, Admin.getadminname ());//question mark 1 APstmt.setstring (2,admin.getadminpassword ());//question Mark 2 -rs = Pstmt.executequery ();//result set -             if(Rs.next ()) { theSYSTEM.OUT.PRINTLN ("Login Successful"); -BL =true; -}Else{ -System.out.println ("User name or password error");  +             } - rs.close (); + pstmt.close (); AConn.close ();//Close Connection at}Catch(SQLException e) { - e.getstacktrace (); -         } -         returnBL; -}
1  Packagecom.entity;2 3  Public classAdmin {4     PrivateString AdminName;5     PrivateString AdminPassword;6     7      PublicAdmin (String adminname,string adminpassword) {8          This. AdminName =AdminName;9          This. AdminPassword =AdminPassword;Ten     } One      A     //Administrator Name -      PublicString Getadminname () { -         returnAdminName; the     } -      Public voidsetadminname (String adminname) { -          This. AdminName =AdminName; -     } +      -     //Administrator Password +      PublicString Getadminpassword () { A         returnAdminPassword; at     } -      Public voidSetadminpassword (String adminpassword) { -          This. AdminPassword =AdminPassword; -     } -}
1     /*********** Test ******************/2      Public Static voidMain (string[] args) {3Connsql sql =NewConnsql ();4Admin admin =NewAdmin ("Amin", "111111");5         BooleanBL =sql.checkadmin (admin);6         if(BL) {7System.out.println ("OK");8}Else{9System.out.println ("Wrong");Ten         } One}

Java connects MySQL database with JDBC (3)-code section

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.