在上篇文章給大家介紹了Myeclipse連接mysql數(shù)據(jù)庫的方法,通過本文給大家介紹如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進(jìn)行簡單的操作,具體詳情請(qǐng)看下文。
創(chuàng)建一個(gè)javaProject,并輸入如下java代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package link; import java.sql.*; /** * 使用JDBC連接數(shù)據(jù)庫MySQL的過程 * DataBase:fuck, table:person; * 使用myeclipse對(duì)mysql數(shù)據(jù)庫進(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,能訪問MySQL數(shù)據(jù)庫的用戶名,密碼;jsj:數(shù)據(jù)庫名 String url = "jdbc:mysql://localhost:/fuck" ; String username = "root" ; String password = "" ; //第二步:創(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對(duì)象類實(shí)例 sql_statement Connection con = getConnection(); Statement sql_statement = con.createStatement(); //如果同名數(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() 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ù)庫中的數(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),密碼需要與你自己的相一致。
這些需要訪問的數(shù)據(jù)必須要與數(shù)據(jù)庫中的類型相互匹配,才能打印出正確的結(jié)果。
右鍵單擊工程名-->Build Path -->Configure Biuld Path -->Libraries --> Add External JARs -->加入一個(gè)jdbc包(具體請(qǐng)查考Mysql的簡單使用(一))--->ok
這時(shí),在包下會(huì)多了一個(gè)Referenced Libraries包文件,則說明配置已經(jīng)成功。
點(diǎn)擊Run as ---> 運(yùn)行Java Application --->JDBCTest--link--->顯示結(jié)果如下:
以上所述是小編給大家介紹的如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進(jìn)行簡單的操作的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!