Java Connection Database code is basically fixed, the procedure is cumbersome, the code to remember for me is very noisy. Directly on the code:
(Warm reminder: Your project is imported into the jar package of the database before the following actions)
1 classdbconnection{2 3 //Driver class name4String driver= "Com.mysql.jdbc.Driver";5 //URL format, and finally the database name6String url= "jdbc:mysql://localhost:3306/javatest? Useunicode=true&characterencoding=utf8 "; //javatest The name of your database 7String user= "Root";8String password= "123456";9Connection coon=NULL;Ten PublicDBConnection () { One Try{ A //Load Driver - Class.forName (driver); -coon=(Connection) drivermanager.getconnection (Url,user,password); the if(!coon.isclosed ()) { -SYSTEM.OUT.PRINTLN ("Connect database successfully! "); - } -}Catch(Exception e) { + e.printstacktrace (); - } + } A Public voidClose () { at Try{ - This. Coon.close (); -}Catch(Exception e) { - e.printstacktrace (); - } - } in //Add Data - Public voidAdd (String name,intage,string Gender) { to //String sql= "insert into Usrinfo (username,gender,age) VALUES (?,?,?)"; Inserting data into the Usrinfo table +String sql= "INSERT into Usrinfo (age,gender,username) VALUES ('" +age+ "', '" +gender+ "', '" +name+ "')"; - Try{ thePreparedStatement prestmt= (PreparedStatement) This. coon.preparestatement (SQL); * //prestmt.setstring (1, name); $ //Prestmt.setint (3, age);Panax Notoginseng //prestmt.setstring (2, gender); And a piece of the above comment to make another insertion method - prestmt.executeupdate (); theSystem.out.println ("Insert data successfully! "); + prestmt.close (); A}Catch(Exception e) { the e.printstacktrace (); + } - } $ $ //Enquiry - Public voidSelect () { -String sql= "SELECT * from Usrinfo"; Querying the information in the Usrinfo table the - Try{WuyiStatement stmt= (Statement) This. Coon.createstatement (); theResultSet rs=(ResultSet) stmt.executequery (SQL); Get a collection of results -System.out.println ("--------------------------------"); WuSystem.out.println ("name" + "\ T" + "age" + "\ T" + "gender"); -System.out.println ("--------------------------------"); About while(Rs.next ()) { $String name=rs.getstring ("username"); - intAge=rs.getint ("Age"); -String gender=rs.getstring ("Gender"); -System.out.println (name+ "\ t" +age+ "\ T" +gender); A } + stmt.close (); the}Catch(Exception e) { - e.printstacktrace (); $ } the } the the //Change Data the - Public voidUpdate (String name,intAge ) { inString sql= "Update usrinfo set age=? where Username=? "; //Recommended Use this way, the following kind of annotation way do not know why sometimes not good to make the //String sql= "Update usrinfo set age=" +age+ "where Username= '" +name+ "'"; the Try{ AboutPreparedStatement prestmt= (PreparedStatement) This. coon.preparestatement (SQL); thePrestmt.setint (1, age); thePrestmt.setstring (2, name); the prestmt.executeupdate (); + - the //Statement stmt= (Statement) this.coon.createStatement ();Bayi //stmt.executeupdate (SQL); theSYSTEM.OUT.PRINTLN ("Change data success! "); the prestmt.close (); -}Catch(Exception e) { - e.printstacktrace (); the } the } the the //Delete Data - Public voiddel (String name) { theString sql= "Delete from Usrinfo where username=?"; the Try{ thePreparedStatement prestmt= (PreparedStatement) This. coon.preparestatement (SQL);94Prestmt.setstring (1, name); the prestmt.executeupdate (); theSystem.out.println ("Delete data successfully! "); the prestmt.close ();98}Catch(Exception e) { About e.printstacktrace (); - }101 }102}
In the main class to create an instance, the instance constructs the method to complete the database connection operation, the instance calls the adding and deleting the method carries on the operation to the database information
1 Public classMysqltest {2 Public Static voidMain (String args[]) {3Scanner in=NewScanner (system.in);4DBConnection db=NewDBConnection ();5 6 //Inserting Data7System.out.println ("Enter name, age, Gender:");8String name=In.next ();9 intAge=in.nextint ();TenString gender=In.next (); One Db.add (name, age, gender); A - //Querying Data - Db.select (); the - //Modifying Data - //String Name=in.next (); - //int age=in.nextint (); + //db.update (name, age); - + A //Delete Data at //String Name=in.next (); - //Db.del (name); - // - //db.close (); - } - in}
Originality is not easy, respect copyright. Reprint Please specify source:http://www.cnblogs.com/xsmile/
Java connection MySQL and additions and deletions to check operation