JDBC Series: (2) Execute SQL statements using statement

Source: Internet
Author: User
Tags stmt

interface for executing SQL statements
Interface function
Statement interface Used to execute a static SQL statement
PreparedStatement interface Used to execute precompiled SQL statements
CallableStatement interface SQL statement for execution of stored procedures (call XXX)


1. Executing DDL statements
package com.rk.db.b_statement;
import java.sql.drivermanager;
import java.sql.connection;
import java.sql.sqlexception;

import java.sql.statement; /**  *  Execute DDL statements with statement (CREATE TABLE)  *  @author  rk  *  */public class 
		Demo01 {static//Static code snippet {try {//1. Driver registration Program Class.forName ("Com.mysql.jdbc.Driver");
			} catch  (Classnotfoundexception e) {e.printstacktrace ();
		Throw new runtimeexception (e);
		}} public static void main (String[] args) {connection conn = null;
		
		statement stmt = null;
		string url =  "Jdbc:mysql://localhost:3306/testdb";
		string user =  "Root";
		
		string password =  "Root";
			
			try {//2. Get Connection Object Conn = drivermanager.getconnection (Url, user, password);
			
			3. Create statement stmt = conn.createstatement (); 4. Prepare SQL String sql =  "Create table t_persons (id int primary key auto_
			
			Increment, username varchar (+),  pwd varchar (20)) ";
			
			5. Send SQL statement, execute SQL statement, get return result int count = stmt.executeupdate (SQL); 6. Output System.out.println ("affects" +count+ "line!)
		");//affects 0 rows!
		} catch  (Sqlexception e) {e.printstacktrace ();
			} finally {//7. Close connection (order: Open first close) closequietly (stmt);
		closequietly (conn); }}/**  *  quiet close  */private static void closequietly (AUTOCLOSEABLE AC) {if (
			Ac != null) {try {ac.close ();
			} catch  (Exception e) {e.printstacktrace (); }
		}
	}
}


2. Executing DML statements
package com.rk.db.b_statement;
import java.sql.drivermanager;
import java.sql.connection;
import java.sql.sqlexception;

import java.sql.statement;
	/**  *  using statement to execute DML statements  *  @author  rk  *  */public class demo02 {
		static {try {//1. Driver registration Program Class.forName ("Com.mysql.jdbc.Driver");
		} catch  (Classnotfoundexception e) {throw new runtimeexception (e);
		}} public static void main (String[] args) {connection conn = null;
		
		statement stmt = null;
		string url =  "Jdbc:mysql://localhost:3306/testdb";
		string user =  "Root";
		
		string password =  "Root";
			
			try {//2. Get Connection Object Conn = drivermanager.getconnection (Url, user, password);
			
			3. Create statement stmt = conn.createstatement (); 4. Prepare SQL string sql =  "INSERT&NBsp;into t_persons (username,pwd)  values (' Earth Man ', ' 123456 ');//Add//string sql =  "UPDATE  t_persons set username= ' Martian '  where id=1 ';//Modify//string sql =  "DELETE  from t_persons where id=1 ";//delete//5. Send SQL statement, execute SQL statement, get return result int count = stmt.
			
			executeupdate (SQL); 6. Output System.out.println ("affects" +count+ "line!)
		");//affects 1 rows!
		} catch  (Sqlexception e) {e.printstacktrace ();
			} finally {closequietly (stmt);
		closequietly (conn); }}/**  *  quiet off  */private static void closequietly (AUTOCLOSEABLE AC) {if (a
			C != null) {try {ac.close ();
			} catch  (Exception e) {e.printstacktrace (); }
		}
	}
}



3. Execute DQL Statement
package com.rk.db.b_statement;
import java.sql.drivermanager;
import java.sql.connection;
import java.sql.sqlexception;
import java.sql.statement;

import java.sql.resultset; /**  *  Execute DQL statement using statement (query operation)  *  @author  rk  *  */public class 
		Demo03 {static {try {//1. Driver registration Program Class.forName ("Com.mysql.jdbc.Driver");
		} catch  (Classnotfoundexception e) {throw new runtimeexception (e);
		}} public static void main (String[] args) {connection conn = null;
		statement stmt = null;
		
		resultset rs = null;
		string url =  "Jdbc:mysql://localhost:3306/testdb";
		string user =  "Root";
		
		string password =  "Root";
			
			try {//2. Get Connection Object Conn = drivermanager.getconnection (Url, user, password); 3. Create statement stmt = conn.Createstatement ();
			
			4. Prepare SQL string sql =  "Select * from t_persons";
			
			5. Send SQL statement, execute SQL statement, get return result rs = stmt.executequery (SQL);
				6. Output while (Rs.next ()) {Int id = rs.getint ("id");
				String username = rs.getstring ("UserName");
				String pwd = rs.getstring ("pwd");
			System.out.println (id +  "\ t"  + userName +  "\ T"  + pwd);
		}} catch  (Sqlexception e) {e.printstacktrace ();
			} finally {closequietly (RS);
			closequietly (stmt);
		closequietly (conn); }}/**  *  quiet off  */private static void closequietly (AUTOCLOSEABLE AC) {if (a
			C != null) {try {ac.close ();
			} catch  (Exception e) {e.printstacktrace (); }
		}
	}
}



4. Mind Mapping


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/F8/wKioL1czKejhkEiuAADsVB6cT98372.png "title=" Jdbc.png "alt=" Wkiol1czkejhkeiuaadsvb6ct98372.png "/>






JDBC Series: (2) Execute SQL statements using statement

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.