如何在Java程序中訪問(wèn)mysql數(shù)據(jù)庫(kù)中的數(shù)據(jù)并進(jìn)行簡(jiǎn)單的操作
在上篇文章給大家介紹了Myeclipse連接mysql數(shù)據(jù)庫(kù)的方法,通過(guò)本文給大家介紹如何在Java程序中訪問(wèn)mysql數(shù)據(jù)庫(kù)中的數(shù)據(jù)并進(jìn)行簡(jiǎn)單的操作,具體詳情請(qǐng)看下文。
創(chuàng)建一個(gè)javaProject,并輸入如下java代碼:
package link;
import java.sql.*;
/**
* 使用JDBC連接數(shù)據(jù)庫(kù)MySQL的過(guò)程
* DataBase:fuck, table:person;
* 使用myeclipse對(duì)mysql數(shù)據(jù)庫(kù)進(jìn)行增刪改查的基本操作。
*/
public class JDBCTest {
public static Connection getConnection() throws SQLException,
java.lang.ClassNotFoundException
{
//第一步:加載MySQL的JDBC的驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
//取得連接的url,能訪問(wèn)MySQL數(shù)據(jù)庫(kù)的用戶名,密碼;jsj:數(shù)據(jù)庫(kù)名
String url = "jdbc:mysql://localhost:/fuck";
String username = "root";
String password = "";
//第二步:創(chuàng)建與MySQL數(shù)據(jù)庫(kù)的連接類的實(shí)例
Connection con = DriverManager.getConnection(url, username, password);
return con;
}
public static void main(String args[]) {
try
{
//第三步:獲取連接類實(shí)例con,用con創(chuàng)建Statement對(duì)象類實(shí)例 sql_statement
Connection con = getConnection();
Statement sql_statement = con.createStatement();
//如果同名數(shù)據(jù)庫(kù)存在,刪除
//sql_statement.executeUpdate("drop table if exists student");
//執(zhí)行了一個(gè)sql語(yǔ)句生成了一個(gè)名為student的表
//sql_statement.executeUpdate("create table student (id int not null auto_increment, name varchar() not null default 'name', math int not null default , primary key (id) ); ");
//向person表中插入數(shù)據(jù)
sql_statement.executeUpdate("insert person values(, 'liying', )");
sql_statement.executeUpdate("insert person values(, 'jiangshan', )");
sql_statement.executeUpdate("insert person values(, 'wangjiawu', )");
sql_statement.executeUpdate("insert person values(, 'duchangfeng', )");
//第四步:執(zhí)行查詢,用ResultSet類的對(duì)象,返回查詢的結(jié)果
String query = "select * from person";
ResultSet result = sql_statement.executeQuery(query);
//顯示數(shù)據(jù)中person表中的內(nèi)容:
System.out.println("person表中的數(shù)據(jù)如下:");
System.out.println("------------------------");
System.out.println("序號(hào)" + " " + "姓名" + " " + "分?jǐn)?shù)");
System.out.println("------------------------");
//對(duì)獲得的查詢結(jié)果進(jìn)行處理,對(duì)Result類的對(duì)象進(jìn)行操作
while (result.next())
{
int number = result.getInt("number");
String name = result.getString("name");
String mathsorce = result.getString("mathsorce");
//取得數(shù)據(jù)庫(kù)中的數(shù)據(jù)
System.out.println(" " + number + " " + name + " " + mathsorce);
}
//關(guān)閉連接和聲明
sql_statement.close();
con.close();
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException");
System.err.println(e.getMessage());
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
注意有幾個(gè)地方是你需要修改的。
如下圖中的url和賬號(hào),密碼需要與你自己的相一致。

這些需要訪問(wèn)的數(shù)據(jù)必須要與數(shù)據(jù)庫(kù)中的類型相互匹配,才能打印出正確的結(jié)果。

右鍵單擊工程名-->Build Path -->Configure Biuld Path -->Libraries --> Add External JARs -->加入一個(gè)jdbc包(具體請(qǐng)查考Mysql的簡(jiǎn)單使用(一))--->ok

這時(shí),在包下會(huì)多了一個(gè)Referenced Libraries包文件,則說(shuō)明配置已經(jīng)成功。

點(diǎn)擊Run as ---> 運(yùn)行Java Application --->JDBCTest--link--->顯示結(jié)果如下:

以上所述是小編給大家介紹的如何在Java程序中訪問(wèn)mysql數(shù)據(jù)庫(kù)中的數(shù)據(jù)并進(jìn)行簡(jiǎn)單的操作的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
navicat不能創(chuàng)建函數(shù)解決方法分享
這篇文章主要介紹了navicat不能創(chuàng)建函數(shù)解決方法分享,小編覺(jué)得還是挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10
mysql8.0及以上my.cnf設(shè)置lower_case_table_names=1無(wú)法啟動(dòng)問(wèn)題
這篇文章主要介紹了mysql8.0及以上my.cnf設(shè)置lower_case_table_names=1無(wú)法啟動(dòng)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
mysql insert if not exists防止插入重復(fù)記錄的方法
在 MySQL 中,插入(insert)一條記錄很簡(jiǎn)單,但是一些特殊應(yīng)用,在插入記錄前,需要檢查這條記錄是否已經(jīng)存在,只有當(dāng)記錄不存在時(shí)才執(zhí)行插入操作,本文介紹的就是這個(gè)問(wèn)題的解決方案。2011-04-04
mysql派生表(Derived Table)簡(jiǎn)單用法實(shí)例解析
這篇文章主要介紹了mysql派生表(Derived Table)簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了mysql派生表的原理、簡(jiǎn)單使用方法及操作注意事項(xiàng),需要的朋友可以參考下2019-12-12
MySQL學(xué)習(xí)必備條件查詢數(shù)據(jù)
這篇文章主要介紹了MySQL學(xué)習(xí)必備條件查詢數(shù)據(jù),首先通過(guò)利用where語(yǔ)句可以對(duì)數(shù)據(jù)進(jìn)行篩選展開主題相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-03-03
Mysql| 使用通配符進(jìn)行模糊查詢?cè)斀?like,%,_)
這篇文章主要介紹了Mysql| 使用通配符進(jìn)行模糊查詢?cè)斀?like,%,_),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
用批處理實(shí)現(xiàn)自動(dòng)備份和清理mysql數(shù)據(jù)庫(kù)的代碼
有網(wǎng)友問(wèn)我在win2003下如何自動(dòng)備份MySQL數(shù)據(jù)庫(kù),既然是自動(dòng)備份,那肯定得寫腳本,當(dāng)然我們也可以利用軟件實(shí)現(xiàn)2013-08-08

