使用JDBC連接MySQL數(shù)據(jù)庫查詢實(shí)例
[原創(chuàng) - 轉(zhuǎn)載請附帶連接 http://www.tkk7.com/myfly/archive/2008/09/18/229802.html ]
[版權(quán):騎豬闖天下]
使用JDBC連接數(shù)據(jù)看查詢主要步驟如下:
1. 加載JDBC驅(qū)動程序;
2. 負(fù)責(zé)管理JDBC驅(qū)動程序的類 DriverManager 會識別加載的驅(qū)動程序,用 DriverManager 類的方法 getConnection()來創(chuàng)建一個(gè)數(shù)據(jù)庫連接類的實(shí)例對象;
3. 獲取Connection對象的實(shí)例,用Connection對象的方法創(chuàng)建一個(gè) Statement 對象實(shí)例,執(zhí)行標(biāo)準(zhǔn)的SQL語句,對數(shù)據(jù)庫、表進(jìn)行相關(guān)操作。
4. 返回的結(jié)果用 ResultSet 類來處理。
程序代碼如下:
package packageOne;

import java.sql.*;


/** *//**
* 使用JDBC連接數(shù)據(jù)庫MySQL的過程
* DataBase:studentinfo, table:student;
* @author DuChangfeng 2008 09 18
*/

public class DataBaseTest
{
public static Connection getConnection() throws SQLException,
java.lang.ClassNotFoundException

{
//第一步:加載MySQL的JDBC的驅(qū)動
Class.forName("com.mysql.jdbc.Driver");
//取得連接的url,能訪問MySQL數(shù)據(jù)庫的用戶名,密碼;studentinfo:數(shù)據(jù)庫名
String url = "jdbc:mysql://localhost:3306/studentinfo";
String username = "root";
String password = "admin";
//第二步:創(chuàng)建與MySQL數(shù)據(jù)庫的連接類的實(shí)例
Connection con = DriverManager.getConnection(url, username, password);
return con;
}

public static void main(String args[])
{
try

{
//第三步:獲取連接類實(shí)例con,用con創(chuàng)建Statement對象類實(shí)例 sql_statement
Connection con = getConnection();
Statement sql_statement = con.createStatement();

/** *//************ 對數(shù)據(jù)庫進(jìn)行相關(guān)操作 ************/
//如果同名數(shù)據(jù)庫存在,刪除
//sql_statement.executeUpdate("drop table if exists student");
//執(zhí)行了一個(gè)sql語句生成了一個(gè)名為student的表
//sql_statement.executeUpdate("create table student (id int not null auto_increment, name varchar(20) not null default 'name', math int not null default 60, primary key (id) ); ");
//向表中插入數(shù)據(jù)
//sql_statement.executeUpdate("insert student values(1, 'liying', 98)");
//sql_statement.executeUpdate("insert student values(2, 'jiangshan', 88)");
//sql_statement.executeUpdate("insert student values(3, 'wangjiawu', 78)");
//sql_statement.executeUpdate("insert student values(4, 'duchangfeng', 100)");
//---以上操作不實(shí)用,但是列出來作為參考---
//第四步:執(zhí)行查詢,用ResultSet類的對象,返回查詢的結(jié)果
String query = "select * from student";
ResultSet result = sql_statement.executeQuery(query);

/** *//************ 對數(shù)據(jù)庫進(jìn)行相關(guān)操作 ************/
System.out.println("Student表中的數(shù)據(jù)如下:");
System.out.println("------------------------");
System.out.println("學(xué)號" + " " + "姓名" + " " + "數(shù)據(jù)成績 ");
System.out.println("------------------------");
//對獲得的查詢結(jié)果進(jìn)行處理,對Result類的對象進(jìn)行操作
while (result.next())

{
int number = result.getInt("id");
String name = result.getString("name");
String mathScore = result.getString("math");
//取得數(shù)據(jù)庫中的數(shù)據(jù)
System.out.println(" " + number + " " + name + " " + mathScore);
}
//關(guān)閉連接和聲明
sql_statement.close();
con.close();

} catch(java.lang.ClassNotFoundException e)
{
//加載JDBC錯(cuò)誤,所要用的驅(qū)動沒有找到
System.err.print("ClassNotFoundException");
//其他錯(cuò)誤
System.err.println(e.getMessage());

} catch (SQLException ex)
{
//顯示數(shù)據(jù)庫連接錯(cuò)誤或查詢錯(cuò)誤
System.err.println("SQLException: " + ex.getMessage());
}
}

}