MyEclipse通過JDBC連接MySQL數(shù)據(jù)庫基本介紹
更新時間:2012年11月12日 10:07:00 作者:
MyEclipse使用Java 通過JDBC連接MySQL數(shù)據(jù)庫的基本測試前提是MyEclipse已經(jīng)能正常開發(fā)Java工程
1.前提是MyEclipse已經(jīng)能正常開發(fā)Java工程
2.安裝MySQL
個人使用的是版本是 mysql-5.0.22-win32.zip
網(wǎng)址:http://www.mysql.com/downloads/mysql/#downloads
3.下載JDBC驅(qū)動
個人使用的是 mysql-connector-java-5.1.22.zip,所需要的就是解壓縮之后其中的 mysql-connector-java-5.1.22-bin.jar
網(wǎng)址:http://www.mysql.com/downloads/connector/j/
4.代碼測試
package ts.jsj.lyh;
import java.sql.*;
/** *//**
* 使用JDBC連接數(shù)據(jù)庫MySQL的過程
* DataBase:JSJ, table:student;
* @author DuChangfeng 2008 09 18
*/
public class JDBCTest {
public static Connection getConnection() throws SQLException,
java.lang.ClassNotFoundException
{
//第一步:加載MySQL的JDBC的驅(qū)動
Class.forName("com.mysql.jdbc.Driver");
//取得連接的url,能訪問MySQL數(shù)據(jù)庫的用戶名,密碼;jsj:數(shù)據(jù)庫名
String url = "jdbc:mysql://localhost:3306/jsj";
String username = "root";
String password = "111";
//第二步:創(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í)行了一個sql語句生成了一個名為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("sno");
String name = result.getString("sname");
String mathScore = result.getString("sgrade");
//取得數(shù)據(jù)庫中的數(shù)據(jù)
System.out.println(" " + number + " " + name + " " + mathScore);
}
//關(guān)閉連接和聲明
sql_statement.close();
con.close();
} catch(java.lang.ClassNotFoundException e) {
//加載JDBC錯誤,所要用的驅(qū)動沒有找到
System.err.print("ClassNotFoundException");
//其他錯誤
System.err.println(e.getMessage());
} catch (SQLException ex) {
//顯示數(shù)據(jù)庫連接錯誤或查詢錯誤
System.err.println("SQLException: " + ex.getMessage());
}
}
}
以上大部分內(nèi)容整理自網(wǎng)絡(luò),感謝猿猿們的無私奉獻(xiàn)~~具體的步驟、強(qiáng)大的互聯(lián)網(wǎng)上都比較容易查詢的到,這里不再贅述,現(xiàn)加上幾點(diǎn)個人認(rèn)為需要注意的地方:
1)關(guān)于mysql-connector-java-5.1.22-bin.jar 的存放位置。在MyEclipse具體的java工程中新建一存放jar 包的文件夾(如 lib),將mysql-connector-java-5.1.22-bin.jar 復(fù)制到文件夾中,選中jar包右擊--->Build Path--->Add To Build Path,即可。
若出現(xiàn)
ClassNotFoundExceptioncom.mysql.jdbc.Driver
的提示,則正是由于缺少導(dǎo)入jar包所造成的。
2)如果已經(jīng)對MySQL的使用很熟悉,則可忽略這條。個人在測試連接時,老是出現(xiàn)這樣的異常提示:
SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
這正是由于個人對MySQL使用不熟悉,對MySQL進(jìn)行了諸多嘗試性的操作,不知何時無意中將MySQL的服務(wù)(如果在安裝MySQL時沒有更改的話,缺省服務(wù)名就是MySQL)關(guān)閉,解決方法開啟此服務(wù)即可??刂泼姘?-->管理工具--->服務(wù)--->MySQL--->選擇啟用。
3)在使用上面的代碼測試時,需要更改的地方有:
//MySQL數(shù)據(jù)庫的用戶名,密碼,數(shù)據(jù)庫名
String url = "jdbc:mysql://localhost:3306/jsj";
String username = "root";
String password = "111";
以及具體基本表中的所要查詢的字段名:
int number = result.getInt("sno");
String name = result.getString("sname");
String mathScore = result.getString("sgrade");
多多分享,有問題歡迎交流~~
2.安裝MySQL
個人使用的是版本是 mysql-5.0.22-win32.zip
網(wǎng)址:http://www.mysql.com/downloads/mysql/#downloads
3.下載JDBC驅(qū)動
個人使用的是 mysql-connector-java-5.1.22.zip,所需要的就是解壓縮之后其中的 mysql-connector-java-5.1.22-bin.jar
網(wǎng)址:http://www.mysql.com/downloads/connector/j/
4.代碼測試
復(fù)制代碼 代碼如下:
package ts.jsj.lyh;
import java.sql.*;
/** *//**
* 使用JDBC連接數(shù)據(jù)庫MySQL的過程
* DataBase:JSJ, table:student;
* @author DuChangfeng 2008 09 18
*/
public class JDBCTest {
public static Connection getConnection() throws SQLException,
java.lang.ClassNotFoundException
{
//第一步:加載MySQL的JDBC的驅(qū)動
Class.forName("com.mysql.jdbc.Driver");
//取得連接的url,能訪問MySQL數(shù)據(jù)庫的用戶名,密碼;jsj:數(shù)據(jù)庫名
String url = "jdbc:mysql://localhost:3306/jsj";
String username = "root";
String password = "111";
//第二步:創(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í)行了一個sql語句生成了一個名為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("sno");
String name = result.getString("sname");
String mathScore = result.getString("sgrade");
//取得數(shù)據(jù)庫中的數(shù)據(jù)
System.out.println(" " + number + " " + name + " " + mathScore);
}
//關(guān)閉連接和聲明
sql_statement.close();
con.close();
} catch(java.lang.ClassNotFoundException e) {
//加載JDBC錯誤,所要用的驅(qū)動沒有找到
System.err.print("ClassNotFoundException");
//其他錯誤
System.err.println(e.getMessage());
} catch (SQLException ex) {
//顯示數(shù)據(jù)庫連接錯誤或查詢錯誤
System.err.println("SQLException: " + ex.getMessage());
}
}
}
以上大部分內(nèi)容整理自網(wǎng)絡(luò),感謝猿猿們的無私奉獻(xiàn)~~具體的步驟、強(qiáng)大的互聯(lián)網(wǎng)上都比較容易查詢的到,這里不再贅述,現(xiàn)加上幾點(diǎn)個人認(rèn)為需要注意的地方:
1)關(guān)于mysql-connector-java-5.1.22-bin.jar 的存放位置。在MyEclipse具體的java工程中新建一存放jar 包的文件夾(如 lib),將mysql-connector-java-5.1.22-bin.jar 復(fù)制到文件夾中,選中jar包右擊--->Build Path--->Add To Build Path,即可。
若出現(xiàn)
ClassNotFoundExceptioncom.mysql.jdbc.Driver
的提示,則正是由于缺少導(dǎo)入jar包所造成的。
2)如果已經(jīng)對MySQL的使用很熟悉,則可忽略這條。個人在測試連接時,老是出現(xiàn)這樣的異常提示:
SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
這正是由于個人對MySQL使用不熟悉,對MySQL進(jìn)行了諸多嘗試性的操作,不知何時無意中將MySQL的服務(wù)(如果在安裝MySQL時沒有更改的話,缺省服務(wù)名就是MySQL)關(guān)閉,解決方法開啟此服務(wù)即可??刂泼姘?-->管理工具--->服務(wù)--->MySQL--->選擇啟用。
3)在使用上面的代碼測試時,需要更改的地方有:
//MySQL數(shù)據(jù)庫的用戶名,密碼,數(shù)據(jù)庫名
復(fù)制代碼 代碼如下:
String url = "jdbc:mysql://localhost:3306/jsj";
String username = "root";
String password = "111";
以及具體基本表中的所要查詢的字段名:
復(fù)制代碼 代碼如下:
int number = result.getInt("sno");
String name = result.getString("sname");
String mathScore = result.getString("sgrade");
多多分享,有問題歡迎交流~~
您可能感興趣的文章:
- Eclipse中C++連接mysql數(shù)據(jù)庫
- myeclipse中連接mysql數(shù)據(jù)庫示例代碼
- Myeclipse連接mysql數(shù)據(jù)庫心得體會
- MyEclipse連接MySQL數(shù)據(jù)庫圖文教程
- Eclipse連接Mysql數(shù)據(jù)庫操作總結(jié)
- MyEclipse連接Mysql數(shù)據(jù)庫的方法(一)
- 傻瓜式用Eclipse連接MySQL數(shù)據(jù)庫
- 用Eclipse連接MySQL數(shù)據(jù)庫的步驟
- MyEclipse連接MySQL數(shù)據(jù)庫報(bào)錯解決辦法
- 教你用eclipse連接mysql數(shù)據(jù)庫
相關(guān)文章
MySQL查看event執(zhí)行記錄的實(shí)現(xiàn)
在使用EVENT的過程中,我們可能會需要查看EVENT的執(zhí)行記錄,以便了解它們是否按預(yù)期執(zhí)行,本文就來介紹一下MySQL查看event執(zhí)行記錄的實(shí)現(xiàn),感興趣的可以了解一下2023-11-11union和子查詢中order?by一起使用導(dǎo)致排序失效問題及解決
這篇文章主要介紹了union和子查詢中order?by一起使用導(dǎo)致排序失效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12MySQL數(shù)據(jù)庫表分區(qū)注意事項(xiàng)大全【推薦】
這篇文章主要介紹了MySQL數(shù)據(jù)庫表分區(qū)注意事項(xiàng)相關(guān)內(nèi)容,比較全面,這里分享給大家,需要的朋友可以參考。2017-10-10MySQL中case?when的兩種基本用法及區(qū)別總結(jié)
在mysql中case when用于計(jì)算條件列表并返回多個可能結(jié)果表達(dá)式之一,下面這篇文章主要給大家介紹了關(guān)于MySQL中case?when的兩種基本用法及區(qū)別的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05MySQL通過實(shí)例化對象參數(shù)查詢實(shí)例講解
在本篇文章里我們給大家分享了關(guān)于MySQL如何通過實(shí)例化對象參數(shù)查詢數(shù)據(jù)的相關(guān)知識點(diǎn)內(nèi)容,有需要的朋友們可以測試參考下。2018-10-10