Java遇上MySQL
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-03-02 08:13:11 閱讀次數(shù):3052次
1、簡(jiǎn)單Java Bean==>可以被序列化。需要準(zhǔn)備的材料為:
①定義n個(gè)私有成員變量,在連接數(shù)據(jù)庫(kù)中,則需要定義與我們所建的表對(duì)應(yīng)的n個(gè)字段。
②實(shí)現(xiàn)全部成員變量的set和get方法。
③實(shí)現(xiàn)無(wú)參數(shù)和帶全參數(shù)的構(gòu)造方法。
④重寫(xiě)toString方法。
⑤實(shí)現(xiàn)接口Serializable。
2、在SQL中,1條記錄像當(dāng)于1個(gè)對(duì)象,所以需要new1個(gè)對(duì)象保存它。
3、在Java中連接數(shù)據(jù)庫(kù)所需的步奏<由于MySQL不是系統(tǒng)自帶的庫(kù),所以需要我們手動(dòng)導(dǎo)包>
①導(dǎo)數(shù)據(jù)庫(kù)包===>建文件夾===>粘貼MySQL數(shù)據(jù)庫(kù)的jar包到文件夾===>右鍵Build Path===>configure Build Path===>Libraries===>AddJARs。
②建class文件。
③前期準(zhǔn)備做足以后,就是我們期待已久的通過(guò)Java連接數(shù)據(jù)庫(kù)了!
A:加載驅(qū)動(dòng)
eg:Class.forName("org.gjt.mm.mysql.Driver"); // org.gjt.mm===>MySQL的特有寫(xiě)法; mysql===>剛剛我們建的文件夾的名字; Driver===>類名
B:連接數(shù)據(jù)庫(kù)
eg:Connection con = null;
String url = "jdbc:mysql://localhost:3306/Lee_My"; // jdbc:mysql===>甚么類型的數(shù)據(jù)庫(kù); localhost===>所連接的數(shù)據(jù)的IP; 3306===>端口號(hào); Lee_My===>數(shù)據(jù)庫(kù)的名字
String user = "root";// 用戶名
String pwd = "123";// 密碼
con = DriverManager.getConnection(url, user, pwd);// 打開(kāi)數(shù)據(jù)庫(kù)
具體實(shí)例看以下代碼:
package com.fs.test;
//import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import com.fs.po.Stu;
public class Test {
private Connection getCon() {// 寫(xiě)1個(gè)連接
數(shù)據(jù)庫(kù)的方法,需要兩步
Connection con = null;
// (1) 加載驅(qū)動(dòng)
try {
Class.forName("org.gjt.mm.mysql.Driver");
// (2)connect連接
數(shù)據(jù)庫(kù) "jdbc:mysql://192.168.1.17:3306/Lee_My";
String url = "jdbc:mysql://localhost:3306/fs_service";
String user = "root";
String pwd = "123";
// 類 DriverManager:管理1組 JDBC 驅(qū)動(dòng)程序的基本服務(wù)。
con = DriverManager.getConnection(url, user, pwd);
// getConnection(String url, String user, String password)
// 試圖建立到給定
數(shù)據(jù)庫(kù) URL 的連接。DriverManager 試圖從已注冊(cè)的 JDBC 驅(qū)動(dòng)程序集當(dāng)選擇1個(gè)適當(dāng)?shù)尿?qū)動(dòng)程序。
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
// insert
private void add() throws Exception {
/** Connection 與特定
數(shù)據(jù)庫(kù)的連接(會(huì)話)。在連接上下文中履行 SQL 語(yǔ)句并返回結(jié)果。*/
Connection con = this.getCon();// 連接
數(shù)據(jù)庫(kù)
Statement st = con.createStatement();// Statement 用于履行靜態(tài) SQL 語(yǔ)句并返回它所生成結(jié)果的對(duì)象。
//接口 Connection 中的 createStatement()方法的作用是:創(chuàng)建1個(gè) Statement 對(duì)象來(lái)將 SQL 語(yǔ)句發(fā)送到
數(shù)據(jù)庫(kù)。
int no = 0;
no = no + st.executeUpdate("insert into stu(name, age, xueHao)values('XiaoMing1', 23, 7)");
no = no + st.executeUpdate("insert into stu(name, age, xueHao)values('XiaoMing2', 24, 8)");
// 接口 Statement 中的 executeUpdate(String sql)方法的作用: 履行給定 SQL 語(yǔ)句,
// 該語(yǔ)句可能為 INSERT、UPDATE 或 DELETE 語(yǔ)句,或不返回任何內(nèi)容的 SQL 語(yǔ)句(如 SQL DDL 語(yǔ)句)。
con.close();// 關(guān)閉流
JOptionPane.showMessageDialog(null, no);// 告知用戶某事已產(chǎn)生,彈出1個(gè)對(duì)話框,對(duì)話框中顯示的消息為no
}
// delete
private void delete() throws Exception {// 同 add()方法
Connection con = this.getCon();
Statement st = con.createStatement();
int no = 0;
no = no + st.executeUpdate("delete from stu where id>1");
con.close();
JOptionPane.showMessageDialog(null, no);
}
private void update() throws Exception {// 同 add()方法
Connection con = this.getCon();
Statement st = con.createStatement();
int no = 0;
no = no + st.executeUpdate("update stu set name='xiaoMing', age=30 where id>1");
con.close();
JOptionPane.showMessageDialog(null, no);
}
private List<Stu> select() throws SQLException {// 創(chuàng)1個(gè)集合List保存查詢出來(lái)的記錄
Connection con = this.getCon();// 連接
數(shù)據(jù)庫(kù)
Statement st = con.createStatement();// Statement 用于履行靜態(tài) SQL 語(yǔ)句并返回它所生成結(jié)果的對(duì)象。
// 接口 Statement 中的executeQuery(sql) 方法履行給定的 SQL 語(yǔ)句,該語(yǔ)句返回單個(gè) ResultSet 對(duì)象。
// 返回:包括給定查詢所生成數(shù)據(jù)的 ResultSet 對(duì)象;永久不能為 null
ResultSet r = st.executeQuery("select id, name, age, xueHao from stu");
List<Stu> list = new ArrayList<Stu>();// 創(chuàng)建1個(gè)ArrayList對(duì)象
// 用while循環(huán)遍歷出各條記錄
while(r.next()) {// r.next()指向下1條記錄,最開(kāi)始指向的是字段,逐次往下移
int id = r.getInt(1);// 輸出id列,即表中第1列
String name = r.getString(2);// 輸出name列,即表中第2列
int age = r.getInt(3);// 輸出age列,即表中第3列
int xueHao = r.getInt(4);// 輸出xueHao列,即表中第4列
list.add(new Stu(id, name, age, xueHao));// 1條記錄就是1個(gè)對(duì)象,所以把各條記錄new個(gè)對(duì)象裝入ArrayList集合中
}
con.close();// 關(guān)閉流
return list;// 返回list集合
}
public static void main(String[] args) throws Exception {
List<Stu> list = new Test().select();
for (Stu stu : list) {// 集合遍歷
System.out.println(stu.toString());
}
}
}
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)