1. 程式人生 > >JDBC簡介(Statement介面)

JDBC簡介(Statement介面)

如果要取得Statement介面例項化物件 需要依靠Connection提供的方法完成:

                 ·取得Statement介面物件:public Statement createStatement()throws SQLException

取得資料庫介面物件後使用以下兩個方法實現資料庫操作:

                ·資料更新:public int executeUpdate()throws SQLException,返回更新行數;

                ·資料查詢:public ResultSet executeQuery()throws SQLException。

範例1:編寫資料庫指令碼

CREATE TABLE member(
  mid NUMBER,
  name VARCHAR2(20),
  birthday DATE DEFAULT SYSDATE,
  age number(3),
  note CLOB,
  CONSTRAINT pk_mid PRIMARY KEY(mid)
);
==============分割線===============

資料更新操作:

範例2:資料增加

        ·INSERT INTO 表名稱(列,列,....)VALUES(值,值,...);

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class OracleDemo {
	public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
	public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:acehzh";
	public static final String USER = "scott";
	public static final String PASSWORD = "tiger";

	public static void main(String[] args) throws Exception {
		// 1.載入資料庫驅動程式,此時不需要例項化,因為容器自己負責管理
		Class.forName(DBDRIVER);
		// 2.連線資料庫
		Connection con = DriverManager.getConnection(DBURL, USER, PASSWORD);
		// 3.進行資料庫操作
		Statement sta = con.createStatement();
		String sql = "INSERT INTO member(mid,name,birthday,age,note) VALUES "
				+ " (myseq.nextval,'張三',TO_DATE('1998-10-10','yyyy-mm-dd'),17,'是個人')";
		int len = sta.executeUpdate(sql);
		System.out.println("影響的行資料:" + len);
		// 4.關閉資料庫
		con.close();
	}

}

==============分割線===============

範例3:資料修改

·語法: UPDATE 表名稱 SET 欄位=值,....WHERE 更新條件(s);

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class OracleDemo {
	public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
	public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:acehzh";
	public static final String USER = "scott";
	public static final String PASSWORD = "tiger";

	public static void main(String[] args) throws Exception {
		// 1.載入資料庫驅動程式
		Class.forName(DBDRIVER);
		// 2.連線資料庫
		Connection con = DriverManager.getConnection(DBURL, USER, PASSWORD);
		// 3.資料庫操作
		Statement sta = con.createStatement();
		// 修改表
		String sql = "UPDATE member SET name='李四',birthday=SYSDATE,age=30 WHERE mid IN(2,4,5,6,7,8,9,10)";
		int len = sta.executeUpdate(sql);
		System.out.println("影響行數:" + len);
		// 4.關閉資料庫
		con.close();
	}

}

==============分割線===============

範例4:刪除資料

·語法:DELETE FORM 表名稱 WHERE 刪除條件(s);

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class OracleDemo {
	public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
	public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:acehzh";
	public static final String USER = "scott";
	public static final String PASSWORD = "tiger";

	public static void main(String[] args) throws Exception {
		// 1.載入資料庫驅動程式
		Class.forName(DBDRIVER);
		// 2.連線資料庫
		Connection con = DriverManager.getConnection(DBURL, USER, PASSWORD);
		// 3.資料庫操作
		Statement sta = con.createStatement();
		// 修改表
		String sql = "DELETE FROM member WHERE mid IN(2,4,6,8,10)";
		int len = sta.executeUpdate(sql);
		System.out.println("刪除成功,影響行數:" + len);
		// 4.關閉資料庫
		con.close();
	}

}