first , make the statement that connects the database into a tool class, because it will always use these sentences
The code is as follows:
PackageCom.swift.jdbc;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException; Public classDbutil { Public StaticConnection Getconn () {Connection conn=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{String URL= "Jdbc:mysql://localhost:3306/sw_database"; String User= "Root"; String Password= "Root"; Conn=drivermanager.getconnection (URL, user, password); } Catch(SQLException e) {e.printstacktrace (); } returnConn; } Public Static voidCloseAll (Connection conn,preparedstatement ps,resultset rs) {if(conn!=NULL) { Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); } } if(ps!=NULL) { Try{ps.close (); } Catch(SQLException e) {e.printstacktrace (); } } if(rs!=NULL) { Try{rs.close (); } Catch(SQLException e) {e.printstacktrace (); } } }}
The tool contains two static methods can be used directly, one is to obtain the connection Getconn get connected Class Connection object, back to connect the database directly dbutil.getconn () can be
And one is to run out of database to close the connection to the database, using Dbutil.closeall (); yes.
next , connect the table Sw_user in database sw_database, display all the users in the console, use the SQL statement for SELECT * from Sw_user;
Output all records from the Sw_user table in the console using four loop methods
The code is as follows:
PackageCom.swift.jdbc;Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;ImportJava.util.Iterator;Importjava.util.LinkedList; Public classQueryallinuser { Public Static voidMain (string[] args) {Connection conn=Dbutil.getconn (); PreparedStatement PS=NULL; ResultSet RS=NULL; LinkedList<User> list =NewLinkedlist<user>(); Try{PS=conn.preparestatement ("SELECT * from Sw_user"); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{RS=Ps.executequery (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try { while(Rs.next ()) {intId=rs.getint ("id"); String username=rs.getstring ("username"); String Password=rs.getstring ("Password"); User User=NewUser (Id,username,password); List.add (user); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } //method One, use the For loop for(intI=0;i<list.size (); i++) {System.out.println (List.get (i)); } System.out.println (); //method Two, using the enhanced for, is equivalent to the foreach for(User user:list) {System.out.println (user.tostring ()); } System.out.println (); //method Three, using While+iteratorIterator<user> it=List.iterator (); while(It.hasnext ()) {System.out.println (It.next ()); } System.out.println (); //method Four, using For+iterator for(Iterator<user> it1=list.iterator (); It1.hasnext ();) {System.out.println (It1.next ()); } }}
The console effect is as follows:
The iterator method and the For method traverse the database user table result set resultset