PreparedStatement interface and resultset result set in Java

Source: Internet
Author: User

Description

The 1.PreparedStatement interface inherits statement, and its instance contains compiled SQL statements that execute faster than statement.

2.PreparedStatement inherits all the features of statement, three methods Executeupdate, ExecuteQuery, and execute no longer require parameters.

3. In JDBC applications, PreparedStatement is generally used instead of statement.


Easy to operate, do some encapsulation first:

To connect the database, close the connection package, in the previous blog has mentioned Dbutil.java;

Encapsulates the database table, which operates on the comp table in my database and is therefore encapsulated as follows:

Package Com.mysqltest.jdbc.modelComp;
	public class Compmember {private int id;
	private String name;
	private int age;

	private double salary; /** * Constructor 1 * @param name * @param age * @param salary/Public Compmember (String name, int age, double salary
		) {super ();
		THIS.name = name;
		This.age = age;
	This.salary = salary; /** * Overloaded constructor * @param ID * @param name * @param age * @param salary/public compmember (int ID, Strin
		g name, int age, double salary) {super ();
		This.id = ID;
		THIS.name = name;
		This.age = age;
	This.salary = salary;
	/** * Get,set Method * * public int getId () {return id;
	The public void setId (int id) {this.id = ID;
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
	public int getage () {return age;
	public void Setage (int age) {this.age = age;
	Public double getsalary () {return salary;
} public void Setsalary (double salary) {		This.salary = salary; @Override/** * Overwrite toString so that the display is better */public String toString () {return "[+this.id+]]" +this.name+ "," +this.age+
	"," +this.salary; }
}


The PreparedStatement interface is then used to achieve increased operations:

Package com.mysqltest.jdbc.xiao1;
Import java.sql.Connection;

Import java.sql.PreparedStatement;
Import Com.mysqltest.jdbc.modelComp.CompMember;

Import Com.mysqltest.jdbc.util.DbUtil;
	
	public class Pstatementtest {private static Dbutil Dbutil = new Dbutil (); /** * Add members with PreparedStatement * @param mem * @return * @throws Exception/private static int AddMember (COMPMEM
		ber mem) throws exception{Connection con = Dbutil.getcon ();
		String sql = "INSERT INTO comp values (null,?,?,?)";
		PreparedStatement pstmt = con.preparestatement (sql);
		Pstmt.setstring (1, Mem.getname ());
		Pstmt.setint (2, Mem.getage ());
		Pstmt.setdouble (3, Mem.getsalary ()); int result = Pstmt.executeupdate ();//middle without incoming SQL Dbutil.close (pstmt, con);
		
	PreparedStatement is a subclass, with the parent class close also line return result;
		public static void Main (string[] args) throws Exception {Compmember mem = new Compmember ("Liu Xiang", 24, 8000.00);
		int result = AddMember (MEM);
	if (result==1) {System.out.println ("add success");	else {System.out.println ("Add failed");
 }
	}
}

Then use the PreparedStatement interface to implement the query and apply the resultset result set:

Package Com.mysqltest.jdbc.xiao2;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.util.ArrayList;

Import java.util.List;
Import Com.mysqltest.jdbc.modelComp.CompMember;

Import Com.mysqltest.jdbc.util.DbUtil;
	
	public class Resultsettest {private static Dbutil Dbutil = new Dbutil ();
		/** * Traversal Query Results * @throws Exception/@SuppressWarnings ("unused") private static void ListMem1 () throws Exception {
		Connection con = Dbutil.getcon ()//get connection String sql = "SELECT * from Comp";
		PreparedStatement pstmt = con.preparestatement (sql); ResultSet rs = pstmt.executequery ()///return result set/next () to move the cursor back one line while (Rs.next ()) {int id = rs.getint (1);//Get the value of the first column I
			D String name = Rs.getstring (2);/int age = Rs.getint (3);
			Double salary = rs.getdouble (4);
			System.out.println ("No.:" + ID + "Name:" + name + "Age:" + aging + "Salary:" + salary);

		System.out.println ("+====================================+"); }/** * Traverse query result method2 * @throws Exception */@SuppressWarnings ("unused") private static void ListMem2 () throws Exception {Connection
		con = Dbutil.getcon ()//get connection String sql = "SELECT * from Comp";
		PreparedStatement pstmt = con.preparestatement (sql); ResultSet rs = pstmt.executequery ()///return result set/Next () takes the cursor back one line while (Rs.next ()) {int id = rs.getint ("id");//Get first column
			Value ID String name = rs.getstring (' name ');/int age = Rs.getint (' age ');
			Double salary = rs.getdouble ("salary");
			System.out.println ("No.:" + ID + "Name:" + name + "Age:" + aging + "Salary:" + salary);

		System.out.println ("+====================================+"); }} private static List<compmember> ListMem3 () throws exception{list<compmember> memlist = new Arrayli
		St<compmember> ();
		Connection con = Dbutil.getcon ()//get connection String sql = "SELECT * from Comp";
		PreparedStatement pstmt = con.preparestatement (sql); ResultSet rs = pstmt.executequery ()///return result set/next () moves the cursor back one line while (Rs.next()) {int id = rs.getint ("id"); Gets the value id String name = rs.getstring ("name") of the first column;/int age = Rs.getint ("Age");
			Double salary = rs.getdouble ("salary");
			Compmember mem = new Compmember (ID, name, age, salary);
		
	Memlist.add (MEM);//Add to List} return memlist;
		public static void Main (string[] args) throws Exception {//ListMem1 ();//listMem2 ();
		list<compmember> memlist = ListMem3 ();
		for (Compmember mem:memlist) {//traversal of each element of the collection System.out.println (MEM);
 }
	}
}



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.