1. 程式人生 > >第十八章:JDBC

第十八章:JDBC

作者:java_wxid

連線資料庫的方式:
第一種方式:ODBC:開放資料庫連線是微軟公司開放服務結構中有關資料庫的一個組成部分,是資料庫訪問介面標準。ODBC是基於C語言實現的。提供了語言和資料庫進行互動的一致性的介面,便於語言和資料庫通訊以及語言對資料庫的各種操作。
第二種方式:JDBC(本章重點)

在Java中,資料庫存取技術可分為如下幾類:
第一種:JDBC直接訪問資料庫
第二種 :JDO技術(Java Data Object)
第三種:第三方O/R工具,如Hibernate, Mybatis 等

JDBC是java訪問資料庫的基石,JDO, Hibernate等只是更好的封裝了JDBC。

什麼是JDBC?
JDBC: Java Data Base Connectivity(java資料庫連線)
它是sun公司提供的一套java應用程式訪問資料庫的技術或規範。是一種用於執行SQL語句的Java API,它統一和規範了應用程式與資料庫的連線、執行SQL語句,併到得到返回結果等各類操作,可以為多種關係資料庫提供統一訪問,它由一組用Java語言編寫的類和介面組成。

JDBC的好處
1、減少了開發程式設計師的壓力,不用去記多套API
2、提高了維護性

如何使用?
第一步:匯入jar包:
1.使用JDBC操作資料庫,需要匯入JDBC的驅動包:mysql-connector-java-5.1.36-bin.jar。
2.在專案下新建libs資料夾,將jar包複製到libs資料夾下面
注意:如果是Dynamic Web Project(動態的web專案)話,則是把驅動jar放到WebContent(有的開發工具叫WebRoot)目錄中的WEB-INF目錄中的lib目錄下即可
3.右鍵–>Build Path–>Add to Build Path,這時,我們可以在專案的引用包中看到我們引用的jar包.

第二步:Java應用程式訪問資料庫的過程:
 ①裝載資料庫驅動程式;
  MySQL的驅動下載地址:http://dev.mysql.com/downloads/
  載入驅動:把驅動類載入到記憶體
  註冊驅動:把驅動類的物件交給DriverManager管理,用於後面建立連線等使用。
  第一種方式:DriverManager.registerDriver(new Driver());//不建議使用
  第二種方式: Class.forName(“com.mysql.jdbc.Driver”);//通過反射,載入與註冊驅動類,解耦合(不直接依賴)
 ②通過JDBC建立資料庫連線;
 ③訪問資料庫,執行SQL語句;
 ④斷開資料庫連線。
 
java.sql包
javax.sql包
此類用於演示JDBC使用的簡單步驟

/**
 * 此類用於演示JDBC使用的簡單步驟
 * 前提:
 * ①需要將mysql-connector-java-5.1.37-bin.jar複製到專案的libs目錄
 * ②右擊mysql-connector-java-5.1.37-bin,Build Path——Add To buildPath
 *
 */
public class TestConnection {
	public static void main(String[] args) throws SQLException {
		// 1.載入驅動(準備工作)
		DriverManager.registerDriver(new Driver());
		// 2.獲取連線
		/*
		 * 引數1:url
		 * 格式:jdbc:mysql://主機名:埠號/庫名
		 * 引數2:使用者名稱
		 * 引數3:密碼
		 */
		Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/customersDB", "root", "root");
		// 3.執行增刪改查★
		//①獲取執行sql語句的命令物件
		Statement statement = connection.createStatement();
		//②執行sql語句
		int update = statement.executeUpdate("delete from customers where id=2");
		//③處理結果
		System.out.println(update>0?"success":"failure");
		// 4.關閉連線
		statement.close();
		connection.close();
	}
}

PreparedStatement的使用

/**
 * 一、向customers表中插入資料,效果如下:
請輸入編號:55
請輸入姓名:上官紅
請輸入郵箱:[email protected]
請輸入生日(要求按xxxx-xx-xx格式):1988-11-11
插入成功!
 */
public class TestPreparedStatementByUtils {
	//增刪改
	@Test 
	public void test1() throws Exception {
		Scanner input = new Scanner(System.in);
		System.out.println("請輸入編號:");
		int id = input.nextInt();
		System.out.println("請輸入姓名:");
		String name = input.next();
		System.out.println("請輸入郵箱:");
		String email = input.next();
		System.out.println("請輸入生日:");
		String birth = input.next();
		//-----------------------------連線資料庫的步驟-----------------
		//1.獲取連線
		Connection connection = JDBCUtils.getConnection();
		//2、執行插入
		PreparedStatement statement = connection.prepareStatement("insert into customers values(?,?,?,?,null)");
		statement.setInt(1, id);
		statement.setString(2, name);
		statement.setString(3, email);
		statement.setString(4, birth);
		int update = statement.executeUpdate();
		System.out.println(update>0?"插入成功":"插入失敗");
		//3.關閉
		JDBCUtils.close(null, statement, connection);
	}
	//查詢
	@Test 
	public void test2() throws Exception {
//		請輸入編號:1
//		---------------------------------------------------------------------------------
//		編號	姓名	郵箱	生日
//		1	汪峰	[email protected]	2010-2-2
		Scanner input = new Scanner(System.in);
		System.out.println("請輸入編號:");
		int id = input.nextInt();
		//-----------------------------連線資料庫的步驟-----------------
		//1.獲取連線
		Connection connection = JDBCUtils.getConnection();
		//2、執行查詢
		PreparedStatement statement = connection.prepareStatement(
				"select id,name,email,birth from customers where id = ?");
		statement.setInt(1, id);
		ResultSet set = statement.executeQuery();
		if(set.next()) {
			String name = set.getString(2);
			String email = set.getString(3);
			String birth = set.getString(4);
			System.out.println(id+"\t"+name+"\t"+email+"\t"+birth);
		}
		//3.關閉
		JDBCUtils.close(set, statement, connection);
	}
}

此類用於演示批處理和PreparedStatement與Statement之間的效率問題

/**
 * 此類用於演示批處理
 * 情況1:多條sql語句的批量執行【較少使用】
 * Statement+批處理:提高了執行的效率,並沒有減少編譯的次數
 * 情況2:一條sql語句的批量傳參【較多使用】
 * PreparedStatement+批處理:減少了執行的次數,也減少了編譯的次數,大大提高效率
 * 相關API:
 * 	addBatch()
 * 	executeBatch()
 * 	clearBatch()
 * 注意:
 * 	①需要在url新增reWriteBatchedStatement = true
 *   驅動類使用的版本:5.1.37、5.1.36但不能使用5.1.7(不支援批處理)
 *  ②插入時,使用values,而不是value!
 *案例:新增50000條管理員記錄
 */
public class TestBatch {
	//沒有使用批處理
	@Test 
	public void testNoBatch() throws Exception {	
		//1.獲取連線
		Connection connection = JDBCUtils.getConnection();		
		//2.執行插入  		
		PreparedStatement statement = connection.prepareStatement("insert into admin values(?,?,?)");		
		for(int i=1;i<=50000;i++) {
			statement.setInt(1, i);
			statement.setString(2, "john"+i);
			statement.setString(3, "0000");    			
			statement.executeUpdate();    			    			
		}    		    		
		//3.關閉
		JDBCUtils.close(null, statement, connection);        		    		
	}
	//使用批處理
	@Test 
	public void testUseBatch() throws Exception {    		
		//1.獲取連線
		Connection connection = JDBCUtils.getConnection();    		
		//2.執行插入    		
		PreparedStatement statement = connection.prepareStatement("insert into admin values(?,?,?)");    		
		for(int i=1;i<=50000;i++) {
			statement.setInt(1, i);
			statement.setString(2, "john"+i);
			statement.setString(3, "0000");   			
			//新增到批處理包(放到框中)
			statement.addBatch();
			if(i%1000==0) {
				statement.executeBatch();//執行批處理包的sql(將框中的蘋果們運到了樓上)
				statement.clearBatch();//清空批處理包的sql(卸貨)
			}
		}
		//3.關閉
		JDBCUtils.close(null, statement, connection);
	}
}

Blob型別資料的讀寫

/**
 * 此類用於演示Blob型別資料的讀寫
 * 注意:只能使用PreparedStatement實現Blob型別的讀寫,不能使用Statement
 * 相關API:
 * 	setBlob(佔位符索引,InputStream)
 * 	InputStream is =getBinaryStream(列索引)
 */
public class TestBlob {
	//測試Blob型別資料的寫入:修改小蒼的照片為指定照片
	@Test 
	public void test1() throws Exception {
		//1.獲取連線
		Connection connection = JDBCUtils.getConnection();	
		//2.執行修改
		PreparedStatement statement = connection.prepareStatement("update customers set photo = ? where name = ?");		
		statement.setBlob(1, new FileInputStream("E:\\beauty\\cang.jpg"));//★		
		statement.setString(2, "小蒼");
		int update = statement.executeUpdate();
		System.out.println(update>0?"插入成功":"插入失敗");		
		//3.關閉
		JDBCUtils.close(null, statement, connection);
	}
	//測試Blob型別資料的讀取:將小蒼的圖片讀取到專案的根目錄下
	@Test 
	public void test2() throws Exception {
		//1.獲取連線
		Connection connection = JDBCUtils.getConnection();		
		//2.執行查詢
		PreparedStatement statement = connection.prepareStatement("select photo from customers where name = ?");
		statement.setString(1, "小蒼");
		ResultSet set = statement.executeQuery();		
		if(set.next()) {
//			Blob blob = set.getBlob(1);
//			InputStream binaryStream = blob.getBinaryStream();			
			InputStream inputStream = set.getBinaryStream(1);
			FileOutputStream fos = new FileOutputStream("src\\beauty.jpg");			
			//邊讀邊寫:複製圖片
			byte[] b = new byte[1024];
			int len;
			while((len=inputStream.read(b))!=-1) {
				fos.write(b, 0, len);				
			}
			fos.close();
			inputStream.close();
		}		
		//3.關閉連線資源		
		JDBCUtils.close(set, statement, connection);
	}
}

##JDBC常用的API

DriverManager驅動管理類
	registDriver 載入驅動【不建議使用】
	getConnection獲取連線【後期往往通過資料庫連線池獲取】

Connection連線介面
	createStatement()獲取命令物件 
	prepareStatement(sql)獲取預編譯命令物件
	close

Statement命令物件介面
	executeUpdate(sql)執行增刪改語句,返回受影響的行數
	executeQuery(sql)執行查詢語句,返回ResultSet物件
	execute(sql)執行任何sql語句,返回是否是結果集
	close

PreparedStatement預編譯命令物件
	executeUpdate()執行增刪改語句,返回受影響的行數
	executeQuery()執行查詢語句,返回ResultSet物件
	execute()執行任何sql語句,返回是否是結果集
	setXX(佔位符的索引,佔位符的值):設定對應占位符的值為XX型別的變數(索引從1開始)
	setObject(佔位符索引,佔位符的值):設定對應占位符的值為Object型別的變數
	close

ResultSet結果集介面
	next()下移一行,指向當前行,返回指向的新行是否有資料
	getXX(columnIndex|columnName)根據列索引或列名獲取XX型別的值
	getObject(columnIndex|columnName)根據列索引或列名獲取Object型別的值	
	previous()上移一行,指向當前行,返回指向的新行是否有資料
	close

##德魯伊連線池的使用

###連線池的好處
1、提高效率
2、提高重用性
3、採用一套統一的管理機制,減少資料庫崩潰問題
###資料庫連線池的使用
javax.sql包下的DataSource是一個數據庫連線池介面。一些開源組織對它進行了實現。
多種開源的資料庫連線池:C3P0,DBCP,Druid,BoneCP,Proxool

###德魯伊使用方式一:

	//建立一個數據庫連線池
	DruidDataSource dds = new DruidDataSource();
	//設定連線引數	
	dds.setUrl("jdbc:mysql://localhost:3306/customersDB");
	dds.setUsername("root");
	dds.setPassword("root");
	dds.setDriverClassName("com.mysql.jdbc.Driver");	
	//設定池子中的初始化連線數
	dds.setInitialSize(5);
	//設定池子中的最大連線數
	dds.setMaxActive(10);
	//設定池子中的最小連線數
	dds.setMinIdle(5);		
	//獲取連線	
	Connection connection = dds.getConnection();	
	System.out.println("connected!");

###德魯伊使用方式二:

	Properties properties = new Properties();	properties.load(this.getClass().getClassLoader().getResourceAsStream("druid.properties"));	
	//1、獲取連線池
	DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
	//2、獲取連線
	Connection connection = dataSource.getConnection();
	System.out.println(connection);	
	//3.關閉連線
	connection.close();

druid.properties

#key=value
driverClassName=com.mysql.jdbc.Driver
#url=jdbc:mysql://localhost:3306/bookstore?rewriteBatchedStatements=true
url=jdbc:mysql://localhost:3306/customersDB
username=root
password=root
initialSize=10
minIdle=5
maxActive=20
maxWait=5000

事務

	@Test 
	public void testTransaction() {		
		//1.獲取連線
		Connection connection = null;
		//事務使用步驟二:編寫事務的語句
				//2.執行增刪改查
		PreparedStatement statement = null;
		try {
			connection = JDBCUtils.getConnection();			
			//事務使用步驟一:開啟事務
			connection.setAutoCommit(false);//取消了事務的自動提交+開啟事務		
			statement = connection.prepareStatement("update account set balance = ? where username = ?");				
			statement.setDouble(1, 995);
			statement.setString(2, "馮紹峰");
			statement.executeUpdate();			
			int i=1/0;			
			//操作2:趙麗穎的錢多5塊				
			statement.setDouble(1, 1005);
			statement.setString(2, "趙麗穎");
			statement.executeUpdate();							
			//事務使用步驟三:結束事務
			connection.commit();//如果執行到該處,說明上面操作沒有異常,則可以正常提交事務
		} catch (SQLException e) {
			try {
				connection.rollback();//如果執行到該處,說明try塊操作有異常,則需要回滾!
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		finally {			
			//3.關閉連線
			try {
				JDBCUtils.close(null, statement, connection);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

批處理

//使用批處理
@Test 
public void testUseBatch() throws Exception {	
	//1.獲取連線
	Connection connection = JDBCUtils.getConnection();
	//2.執行插入
	PreparedStatement statement = connection.prepareStatement("insert into admin values(?,?,?)");	
	for(int i=1;i<=50000;i++) {
		statement.setInt(1, i);
		statement.setString(2, "john"+i);
		statement.setString(3, "0000");			
		//新增到批處理包(放到框中)
		statement.addBatch();
		if(i%1000==0) {
			statement.executeBatch();//執行批處理包的sql(將框中的蘋果們運到了樓上)
			statement.clearBatch();//清空批處理包的sql(卸貨)
		}
	}
	//3.關閉
	JDBCUtils.close(null, statement, connection);
}

自己動手封裝一個BasicDao

/**
 * 自己動手封裝一個BasicDao
 *  功能:
 *  1、通用的增刪改
 *  	針對於任何表的增刪改語句
 *  2、通用的查詢單條(返回的是一行資訊)
 *  	針對於任何表的任何查詢單條的記錄
 *  	返回型別:T 物件
 *  	orm思想:一個表對映成一個類,一條記錄對映成一個物件,一個欄位對映成一個屬性
 *  	引數:Class clazz,String sql,Object...objects
 *  3、通用的查詢多條(返回的是多行資訊)
 *  	針對於 任何表的任何查詢多條的記錄
 *  	返回型別:List<T>
 *  	引數:Class clazz,String sql,Object...objects
 */
public class BasicDaoBySelf {
	/**
	 * 功能:針對於任何表的查詢多條
	 * @param clazz
	 * @param sql
	 * @param objects
	 * @return List<T>
	 */
	public <T> List<T> queryMulti(Class<T> clazz,String sql,Object...objects){
		Connection connection  = null;
		PreparedStatement statement = null;
		ResultSet set = null;
		try {
			connection  = JDBCUtils.getConnection();
			statement = connection.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				statement.setObject(i+1, objects[i]);
			}
			set = statement.executeQuery();
			ResultSetMetaData metaData = set.getMetaData();
			List<T> list = new ArrayList<>();//集合
			int columnCount = metaData.getColumnCount();
			while(set.next()) {
				T t = clazz.newInstance();
				for(int i=1;i<=columnCount;i++) {
					//獲取屬性名(列名)
					String fieldName = metaData.getColumnLabel(i);
					//獲取屬性值(列的值)
					Object value = set.getObject(i);
					Field field = clazz.getDeclaredField(fieldName);
					field.setAccessible(true);
					field.set(t, value);
				}
				list.add(t);
			}
			return list;
		}catch(Exception e) {
			throw new RuntimeException(e);
		}finally {
			JDBCUtils.close(set, statement, connection);
		}
	}
	/**
	 * 功能:執行任何表的查詢單條記錄的sql語句
	 * @param clazz 錶針對的類的Class物件
	 * @param sql 待執行的sql
	 * @param objects 佔位符
	 * @return T 型別的物件(表中的一條記錄以物件的形式返回)
	 * @throws Exception 
	 */
	public<T> T querySingle(Class<T> clazz,String sql,Object...objects)  {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet set = null;
		try {
			//1.獲取連線
			 connection = JDBCUtils.getConnection();
			//2.執行查詢
			statement = connection.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				statement.setObject(i+1, objects[i]);
			}
			set = statement.executeQuery();
			/*
			 * 一、元資料結果集的使用
			 * ResultSet:儲存結果集的資料
			 * 元資料結果集:解剖ResultSet的結構
			 * 二、反射的使用
			 * 封裝物件:
			 * 正常的方式:
			 * ①呼叫無參構造建立物件
			 * 類名 物件名  = new 類名();
			 * ②為物件的各個屬性賦值
			 * 物件名.屬性名=值;
			 * 反射的方式:
			 * 建立物件:
			 * 	T t= clazz.newInstance();
			 * 為屬性賦值:
			 * 	Field field = t.getDecalaredField(屬性名);
			 *  field.setAccessible(true);
			 *  field.set(t,value);
			 * 三、泛型的使用
			 */
			//獲取元資料結果集
			ResultSetMetaData metaData = set.getMetaData();
			//結果集的列數
			int columnCount = metaData.getColumnCount();
			if(set.next()) {
				T t= clazz.newInstance();
				for(int i=1;i<=columnCount;i++) {//遍歷列數
					String columnLabel = metaData.getColumnLabel(i);//根據列索引獲取列名或別名(屬性名)
					Object value = set.getObject(i);//拿到該列的值(各個屬性的值)
					Field field = clazz.getDeclaredField(columnLabel);
					field.setAccessible(true);
					field.set(t,value);
				}
				return t;
			}
			return null;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		finally {
			JDBCUtils.close(set, statement, connection);
		}
	}
	/**
	 * 功能:通用的增刪改語句
	 * @param sql 待執行的sql,裡面可能有佔位符
	 * @param objects 佔位符的值
	 * @return 受影響的行數
	 * @throws SQLException 
	 * @throws Exception 
	 */
	public int update(String sql,Object...objects) {
		//1.獲取連線
		Connection connection= null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtils.getConnection();
			//2.執行sql
			statement = connection.prepareStatement(sql);
			for(int i=0;i<objects.length;i++) {
				statement.setObject(i+1, objects[i]);
			}
			int update = statement.executeUpdate();
			return update;
		}  catch (Exception e) {
			throw new RuntimeException(e);//將編譯異常轉換成執行異常
		}
		finally {
			//3.關閉
			JDBCUtils.close(null, statement, connection);
		}
	}
}

00000000000000000000000000000000000000000000000000000000000000000000
使用JDBCUTils工具類自己寫一個增刪改,查詢單條,多條記錄,查詢單個值的通用方法

/**
 * 此類用於演示DBUTils的使用
 * QueryRunner
 * 		update
 * 		query
 * ResultSetHandler
 * 		BeanHandler
 * 		BeanListHandler
 * 		ScalarHandler
 */
public class BasicDaoByUtils<T> {
	QueryRunner qr = new QueryRunner();
	/**
	 * 通用的增刪改
	 * @param sql
	 * @param objects
	 * @return
	 */
	public int update(String sql,Object...objects) {
		Connection connection  = null;
		try {
			connection  = JDBCUtils.getConnection();
			return  qr.update(connection, sql, objects);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		finally {
			JDBCUtils.close(null, null, connection);
		}
	}
	/**
	 * 功能:查詢單條
	 * @param clazz
	 * @param sql
	 * @param objects
	 * @return
	 */
	public T querySingle(Class<T> clazz,String sql,Object...objects) {
		Connection connection  = null;
		try {
			connection  = JDBCUtils.getConnection();			
			return qr.query(connection, sql, new BeanHandler<T>(clazz),objects);		
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		finally {
			JDBCUtils.close(null, null, connection);
		}
	}	
	/**
	 * 功能:查詢多條
	 * @param clazz
	 * @param sql
	 * @param objects
	 * @return
	 */
	public List<T> queryMulti(Class<T> clazz,String sql,Object...objects) {
		Connection connection  = null;
		try {
			connection  = JDBCUtils.getConnection();			
			return qr.query(connection, sql, new BeanListHandler<T>(clazz),objects);			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		finally {
			JDBCUtils.close(null, null, connection);
		}
	}
	/**
	 * 功能:查詢單個值
	 * @param sql
	 * @param objects
	 * @return
	 */	
	public Object scalar(String sql,Object...objects) {
		Connection connection  = null;
		try {
			connection  = JDBCUtils.getConnection();			
			return qr.query(connection, sql, new ScalarHandler(),objects);			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		finally {
			JDBCUtils.close(null, null, connection);
		}
	}
}

JDBCUtils工具類

/**
 * jdbc工具類
 */
public class JDBCUtils {
	static DataSource dataSource;
	static {
		try {
			Properties properties = new Properties();
			properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
			 dataSource = DruidDataSourceFactory.createDataSource(properties);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	//使用druid獲取連線
	public static Connection getConnection() throws Exception  {
		return dataSource.getConnection();
	}
	//使用c3p0獲取連線
	public static Connection getConnection2() throws SQLException {
		ComboPooledDataSource cpds = new ComboPooledDataSource("hello");
		return cpds.getConnection();
	}
	//釋放連線資源
	public static void close(ResultSet set,Statement statement,Connection connection){
		try {
			if(set!=null)
				set.close();
			if(statement!=null)
				statement.close();
			if(connection!=null)
				connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}    		
	}
}

測試工具類的使用情況

/**
 * 此類用於測試工具類的使用
 */
public class TestDBUtils {
	@Test 
	public void test1() {
		BasicDaoByUtils<Admin> bd = new BasicDaoByUtils<>();
		int update = bd.update("update admin set username = ? where id =?", "小花",2);
		System.out.println(update>0?"success":"failure");
	}    	
	@Test 
	public void test2() {
		BasicDaoByUtils<Admin> bd = new BasicDaoByUtils<>();
//		Admin querySingle = bd.querySingle(Admin.class, "select * from Admin where id=?", 2);//Admin自己新建的實體類
//		System.out.println(querySingle);
		List<Admin> list = bd.queryMulti(Admin.class, "select * from Admin where id<?", 10);
		for (Admin admin : list) {
			System.out.println(admin);
		}
		Object scalar = bd.scalar("select count(*) from admin");
		System.out.println(scalar);
	}
}

00000000000000000000000000000000000000000000000000000000000000000000
JDBC相關的API
一、Connection介面:

1.createStatement():建立資料庫連線
2.prepareStatement(String sql):建立預處理語句
3.prepareCall(String sql):建立可呼叫語句
4.getAutoCommit():獲取自動提交的模式
5.setAutoCommit():設定自動提交的模式
6.commit():提交所執行的SQL語句
7.rollback():回滾所執行的SQL語句
8.getMetaData():獲取一個DatabaseMetaData物件,該物件包含了有關資料庫的基本資訊
9.close():關閉資料庫連線
10.isClose():判斷資料庫連線是否超時或被顯示關閉

二、Statement介面:

1.execute(String sql):執行SQL語句,如果返回值是結果集則為true,否則為false
2.executeQuery(String sql):執行SQL語句,返回值為ResultSet
3.executeUpdate(String sql):執行SQL語句,返回值為所影響的行數
4.addBatch(String sql):向當前Statement物件的命令列表中新增新的批處理SQL語句
5.clearBatch():清空當前Statement物件的命令列表
6.executeBatch():執行當前Statement物件的批處理語句,返回值為每個語句所影響的函式陣列
7.getConnection():返回建立了該Statement物件的Connection物件
8.getQueryTimeout():獲取等待處理結果的時間
9.setQueryTimeout():設定等待處理結果的時間

三、ResultSet介面:

1.first()/beforeFirst():將遊標移動到ResultSet中第一條記錄(的前面)
2.last()/afterLast():將遊標移動到ResultSet中最後一條記錄(的後面)
3.absolute(int column):將遊標移動到相對於第一行的指定行,負數則為相對於最後一條記錄
4.relative(int rows):將遊標移動到相對於當前行的第幾行,正為向下,負為向上
5.next():將遊標下移一行
6.previous():將遊標上移一行
7.insertRow():向當前ResultSet和資料庫中被插入行處插入一條記錄
8.deleteRow():將當前ResultSet中的當前行和資料庫中對應的記錄刪除
9.updateRow():用當前ResultSet中已更新的記錄更新資料庫中對應的記錄
10.cancelUpdate():取消當前對ResultSet和資料庫中所做的操作
11.findColumn(String columnName):返回當前ResultSet中與指定列名對應的索引
12.getRow():返回ResultSet中的當前行號
13.refreshRow():更新當前ResultSet中的所有記錄
14.getMetaData():返回描述ResultSet的ResultSetMetaData物件
15.isAfterLast(): 是否到了結尾
16.isBeforeFirst(): 是否到了開頭
17.isFirst():是否第一條記錄   
18.isLast(): 是否最後一條記錄
19.wasNull():檢查列值是否為NULL值,如果列的型別為基本型別,且資料庫中的值為0,那麼這項檢查就很重要。由於資料庫NULL也返回0,所以0值和資料庫的NULL不能區分。如果列的型別為物件,可以簡單地將返回值與null比較  
20.close():關閉當前ResultSet

四、ResultSetMetaData介面:

1.getColumnCount():返回ResultSet中列的數目
2.getColumnName():返回列在資料庫中的名稱
3.getColumnType():返回列的SQL型別
4.isReadOnly():表示該資料項是否為只讀值
5.isNullable():表示該列是否可以儲存NULL