如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作
在上篇文章給大家介紹了Myeclipse連接mysql數(shù)據(jù)庫的方法,通過本文給大家介紹如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作,具體詳情請看下文。
創(chuàng)建一個javaProject,并輸入如下java代碼:
package link; import java.sql.*; /** * 使用JDBC連接數(shù)據(jù)庫MySQL的過程 * DataBase:fuck, table:person; * 使用myeclipse對mysql數(shù)據(jù)庫進行增刪改查的基本操作。 */ 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:/fuck"; String username = "root"; String password = ""; //第二步:創(chuàng)建與MySQL數(shù)據(jù)庫的連接類的實例 Connection con = DriverManager.getConnection(url, username, password); return con; } public static void main(String args[]) { try { //第三步:獲取連接類實例con,用con創(chuàng)建Statement對象類實例 sql_statement Connection con = getConnection(); Statement sql_statement = con.createStatement(); //如果同名數(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() 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類的對象,返回查詢的結(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("序號" + " " + "姓名" + " " + "分數(shù)"); System.out.println("------------------------"); //對獲得的查詢結(jié)果進行處理,對Result類的對象進行操作 while (result.next()) { int number = result.getInt("number"); String name = result.getString("name"); String mathsorce = result.getString("mathsorce"); //取得數(shù)據(jù)庫中的數(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()); } } }
注意有幾個地方是你需要修改的。
如下圖中的url和賬號,密碼需要與你自己的相一致。
這些需要訪問的數(shù)據(jù)必須要與數(shù)據(jù)庫中的類型相互匹配,才能打印出正確的結(jié)果。
右鍵單擊工程名-->Build Path -->Configure Biuld Path -->Libraries --> Add External JARs -->加入一個jdbc包(具體請查考Mysql的簡單使用(一))--->ok
這時,在包下會多了一個Referenced Libraries包文件,則說明配置已經(jīng)成功。
點擊Run as ---> 運行Java Application --->JDBCTest--link--->顯示結(jié)果如下:
以上所述是小編給大家介紹的如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作的相關(guān)知識,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
navicat不能創(chuàng)建函數(shù)解決方法分享
這篇文章主要介紹了navicat不能創(chuàng)建函數(shù)解決方法分享,小編覺得還是挺不錯的,這里分享給大家,供需要的朋友參考。2017-10-10mysql8.0及以上my.cnf設(shè)置lower_case_table_names=1無法啟動問題
這篇文章主要介紹了mysql8.0及以上my.cnf設(shè)置lower_case_table_names=1無法啟動問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11mysql insert if not exists防止插入重復(fù)記錄的方法
在 MySQL 中,插入(insert)一條記錄很簡單,但是一些特殊應(yīng)用,在插入記錄前,需要檢查這條記錄是否已經(jīng)存在,只有當(dāng)記錄不存在時才執(zhí)行插入操作,本文介紹的就是這個問題的解決方案。2011-04-04mysql派生表(Derived Table)簡單用法實例解析
這篇文章主要介紹了mysql派生表(Derived Table)簡單用法,結(jié)合實例形式分析了mysql派生表的原理、簡單使用方法及操作注意事項,需要的朋友可以參考下2019-12-12MySQL學(xué)習(xí)必備條件查詢數(shù)據(jù)
這篇文章主要介紹了MySQL學(xué)習(xí)必備條件查詢數(shù)據(jù),首先通過利用where語句可以對數(shù)據(jù)進行篩選展開主題相關(guān)內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你有所幫助2022-03-03Mysql| 使用通配符進行模糊查詢詳解(like,%,_)
這篇文章主要介紹了Mysql| 使用通配符進行模糊查詢詳解(like,%,_),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08用批處理實現(xiàn)自動備份和清理mysql數(shù)據(jù)庫的代碼
有網(wǎng)友問我在win2003下如何自動備份MySQL數(shù)據(jù)庫,既然是自動備份,那肯定得寫腳本,當(dāng)然我們也可以利用軟件實現(xiàn)2013-08-08