Public Static voidMain (string[] args)throwsexception{Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn= Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb", "Root", "" "); String SQL= "SELECT * FROM Info"; //when you associate a query, you can join on directly. but not very efficient.//String sql = "Select Info." Code,info. Name,info. Sex,nation. Name SB, Info. Birthday from info joins nation on info. Nation=nation. Code ";Statement State=conn.createstatement (); ResultSet RS=state.executequery (SQL); while(Rs.next ()) {//determine if there is a next lineSystem.out.print (rs.getstring (1) + "\ T"); System.out.print (Rs.getstring (2) + "\ T"); System.out.print (Rs.getboolean (3)? " Male \ T ":" Female \ T ");//?: Simple JudgmentSystem.out.print (Minzu (rs.getstring (4)) + "\ T"); System.out.println (Bianhuan (Rs.getdate (5))); } conn.close (); } //when you associate a query, you can also write a method to check the other table again, and then assign the original column Private StaticString Minzu (String m)throwsException {String mz= "";//defining an empty stringClass.forName ("Com.mysql.jdbc.Driver"); Connection Conn= Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb", "Root", "" "); Statement STA=conn.createstatement (); String SQL= "SELECT * from Nation where code = '" +m+ "'"; ResultSet RS=sta.executequery (SQL); if(Rs.next () = =true){//when there is a corresponding SQL statement, execute theMZ= Rs.getstring (2);//the column of another table is assigned to MZ} conn.close (); returnMz//back to MZ } //Date Time Conversion Public StaticString Bianhuan (Date d) {SimpleDateFormat F=NewSimpleDateFormat ("yyyy mm month DD Day"); returnF.format (d); }
Results:
JDBC MySQL Multi-Table association query Query