How to query all records using JDBC

Source: Internet
Author: User

public class Jdbcdao {
Private Connection Conn=null; Database Connection Object
Private String Strsql=null;

JDBC Connection Database
Public Jdbcdao () {

String Driver = "Com.mysql.jdbc.Driver";
try {
Class.forName (driver);
String url = "Jdbc:mysql://localhost:3306/dev?characterencoding=utf8";
Conn=drivermanager.getconnection (URL, "root", "");
SYSTEM.OUT.PRINTLN ("Connect MySQL database successfully");

} catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}
}

Query all student information and query all data for a table
public void query (Student Student) {
Statement Stmt=null; SQL Statement Object
ResultSet Rs=null; Saving result set objects
Student stu=new Student (); Instantiating student objects
try {
Strsql= "SELECT * from student"; Querying SQL statements
Stmt=conn.createstatement (Rs. Type_scroll_insensitive,rs. CONCUR_READ_ONLY);
Execute SQL statement
Rs=stmt.executequery (strSQL); return result set
while (Rs.next ()) {//loops through the result set, takes a value, sets the Student object property value.
Stu.setid (Rs.getint ("id"));
Stu.setname (rs.getstring ("name"));
Stu.setphone (rs.getstring ("Phone"));
Stu.setstuno (rs.getstring ("Stuno"));
Stu.setbirthday (rs.getstring ("Brithday"));
SYSTEM.OUT.PRINTLN ("Query Success" +strsql);
}
} catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}

SYSTEM.OUT.PRINTLN ("Query data Success" +strsql);

}

How to query all records using JDBC

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.