Java in eclipse gets the query result set for MySQL

Source: Internet
Author: User
Tags stmt

No nonsense, first on the code, then explain the explanation

1PackageCom.ningmeng;23Import java.sql.*;4/**5* 1: Get query result set6*@authorBiexiansheng7*8*/9PublicClassTest03 {1011PublicStaticvoidMain (string[] args) {12Try{Class.forName ("Com.mysql.jdbc.Driver");SYSTEM.OUT.PRINTLN ("Load Database driver succeeded");String url= "Jdbc:mysql://localhost:3306/test";//Declaring the URL of the database testString user= "root";//Database accountPass= "123456";//Database Password18//Establish a database connection and get the connection object connConnection conn=Drivermanager.getconnection (Url,user,pass);SYSTEM.OUT.PRINTLN ("Database connection succeeded");Statement stmt=conn.createstatement ();//Create a statement objectString sql= "SELECT * from users";//Generate an SQL statementResultSet rs=stmt.executequery (SQL);//Execute the query and assign the query result to the result set object24int id,age,sex;//Declare 3 variables as Id,age,sexString Username,password;//Declare 2 variables for user name, passwordSystem.out.println ("id\t user name \ t password \ t age \ t gender");//where \ t equals 8 spaces27while (Rs.next ()) {//Traversing result SetsId=rs.getint ("id");//Get IDUsername=rs.getstring (2);//Password=rs.getstring ("password");//Age=rs.getint (4);//Sex=rs.getint (5);//System.out.println (id+ "\ t" +username+ "\ T" +password+ "\ T" +age+ "\ T" +sex+ "\ T");35}SYSTEM.OUT.PRINTLN ("Get query result set");37Conn.close ();SYSTEM.OUT.PRINTLN ("Close Database Connection object");39}Catch(ClassNotFoundException e) {40// TODO auto-generated catch block e.printstacktrace ();      (SQLException e) {$/ / TODO auto-generated catch block45)//LOAD Database driver c14> E.printstacktrace ();               +} 

The results are as follows

The 1:result interface is similar to a temporary table used to temporarily hold the result set obtained by a database query operation.

The Excutequery () method in the 2:preparedstatement interface, in which the PreparedStatement object executes a SQL query statement that returns the result as a result set of query results object

3:next () Moves the pointer down one line

The GetXXX () method of the 4:resultset object gets the data in the query result set. Because the data saved in ResultSet is in the form of a table, you can specify the column ordinal and column name by using the GetXXX () method.

Id=rs.getint ("id");//Get ID

Username=rs.getstring (2);//

Password=rs.getstring ("password");//

Age=rs.getint (4);//

Sex=rs.getint (5);//

You can understand it carefully, GetXXX (parameters), the parameter can be either the name of the column or the number of the first column.

Java in eclipse gets the query result set for MySQL

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.