Java Database--resultset Interface

Source: Internet
Author: User

The full results of the database can be queried using the SELECT statement in SQL, and all query records for the database are received using ResultSet in JDBC operations, and the content is displayed using ResultSet.

Querying data from the user table

=================================================//File name:mysql_demo//------------------------------------ ------------------------------------------//Author:commonimport java.sql.*;//main class//function:mysql_demopu Blic class Mysql_demo {//Database driver to define MySQL public static final String dbdriver = "org.gjt.mm.mysql.Driver";// Define the connection address of the MySQL database public static final String Dburl = "Jdbc:mysql://localhost:3306/mysql_demo"; connection user name for//mysql database public Static final String DBUSER = "root"; connection password for//mysql database public static final String dbpass = "123456";p ublic static void Main (St Ring[] args) throws Exception {//TODO automatically generated method stub connection conn = null;//database connection Statement stmt = null;//database operation ResultSet rs = null;//save query result String sql = "Select id,name,password,age,sex,birthday from User"; Class.forName (dbdriver);//Load driver//connection to MySQL database, write the username and password of the connection conn = Drivermanager.getconnection (dburl,dbuser,dbpass ); stmt = Conn.createstatement ();//Instantiate Statement Object rs = stmt.executequery (sql);//Instantiate ResultSet object while (Rs.nexT ()) {int id = rs.getint ("id"); String name = rs.getstring ("name"); String pass = rs.getstring ("password"); int = Rs.getint ("Age"); String sex = rs.getstring ("Sex");D ate d = rs.getdate ("Birthday"); System.out.println ("Number:" +id); System.out.println ("Name:" +name); System.out.println ("Password:" +pass); System.out.println ("Age:" +age); System.out.println ("Gender:" +sex); System.out.println ("Birthday:" +d);} Rs.close (); Close result set Stmt.close (); Operation off Conn.close (); Database Close}}

Java Database--resultset Interface

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.