JDBC MySQL Multi-Table association query Query

Source: Internet
Author: User

 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

Related Article

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.