如何在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)文章
解決MySQL報(bào)錯(cuò)1267 - Illegal mix of coll
這篇文章主要給大家介紹了解決MySQL報(bào)錯(cuò)1267 - Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPL的問(wèn)題,文中有詳細(xì)的解決方案,需要的朋友可以參考下2023-09-09
使用mysqld_multi實(shí)現(xiàn)單系統(tǒng)中配置多個(gè)MySQL服務(wù)器實(shí)例
這篇文章主要介紹了使用mysqld_multi實(shí)現(xiàn)單系統(tǒng)中配置多個(gè)MySQL服務(wù)器實(shí)例,mysqld_multi可用于在一臺(tái)物理服務(wù)器運(yùn)行多個(gè)Mysql服務(wù),mysqld_multi是管理多個(gè)mysqld的服務(wù)進(jìn)程,這些服務(wù)進(jìn)程程序不同的unix socket或是監(jiān)聽(tīng)于不同的端口,需要的朋友可以參考下2014-09-09
MySQL?insert死鎖問(wèn)題解決詳細(xì)記錄
上周遇到一個(gè)因insert而引發(fā)的死鎖問(wèn)題,其成因比較令人費(fèi)解,下面這篇文章主要給大家介紹了關(guān)于MySQL?insert死鎖問(wèn)題解決詳細(xì)記錄的相關(guān)資料,需要的朋友可以參考下2022-11-11
MySQL中on?duplicate?key?update的使用方法實(shí)例
在做數(shù)據(jù)統(tǒng)計(jì)的時(shí)候,我們經(jīng)常會(huì)用到mysql的on duplicate key update語(yǔ)法來(lái)自動(dòng)更新數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MySQL中on?duplicate?key?update的使用方法的相關(guān)資料,需要的朋友可以參考下2022-09-09
Mysql8.0密碼問(wèn)題mysql_native_password和caching_sha2_password詳解
這篇文章主要介紹了Mysql8.0密碼問(wèn)題mysql_native_password和caching_sha2_password,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

