JDBC連接過程:
1.導入相關的JDBC驅動jar包;
①:新建一個文件夾,命名爲lib;
②:將jar包粘貼至此文件夾;
③:右鍵jar包,Add to Build Path;
④:完成。
2.註冊驅動,把mysql的Driver對象註冊到java.sql.Drivermanager中;
Class.forName("com.mysql.jdbc.Driver");
3.建立連接;
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
4.SQL業務操作;
首先簡單介紹一下連接的數據庫book:
只有兩個表:user和bookmessage。
①:user
②:bookmessage
uid爲外鍵,下面的業務操作只用到表bookmessage。
①:Statement;
(1)查詢操作:
Statement stmt = null;
ResultSet rs = null;
List<book> list = new ArrayList<book>();
String sql ="select * from bookmessage";
rs = stmt.executeQuery(sql);//查詢
while(rs.next()) {
int bid = rs.getInt(1);//bid數據類型爲int
String name = rs.getString(2);//bookname數據類型爲String
int uid = rs.getInt(3);//uid數據類型爲int
//System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getInt(3));直接打印表的信息;
book b = new book(bid,name,uid);//構建book類打印表的信息
list.add(b);//加入book類list
}
//打印
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i).getBid()+"\t"+list.get(i).getBookname()+"\t\t"+list.get(i).getUid());
}
book類:
package JDBC;
public class book {
private int bid;
private String bookname;
private int uid;
public book() {
}
public book(int bid,String bookname,int uid) {
this.bid = bid;
this.bookname = bookname;
this.uid = uid;
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
}
(2)刪除操作:
Statement stmt = null;
String sql ="delete from bookmessage where bid =6";
stmt = conn.createStatement();
int v = stmt.executeUpdate(sql);//添加 修改 刪除時 返回受影響的行數;
if(v>0) {
System.out.println("success");
}else {
System.out.println("fail");
}
添加和修改就不一一進行演示了,接下來介紹PreparedStatement。
②:PreparedStatement。
PreparedStatement有預編譯功能,可以防止sql注入,安全性更高。
舉一個添加的例子:
String sql ="insert into bookmessage values(?,?,?)";
int bid = 7;
String bookname ="林清玄散文精選";
int uid = 3;
pstmt = conn.prepareStatement(sql);
//給定參數位置從左到右依次爲1,2,3,4 ~~
pstmt.setInt(1,bid);//第一個?處設置參數bid
pstmt.setString(2,bookname);//第二個?處設置參數bookname
pstmt.setInt(3,uid);//第三個?處設置參數uid
int v = pstmt.executeUpdate();
if(v>0) {
System.out.println("success");
}else {
System.out.println("fail");
}
5.關閉資源。
執行完sql業務後,關閉使用的資源,要進行判空。
if(null != rs) {
rs.close();
}
if(null != stmt) {
stmt.close();
}
if(null != pstmt) {
pstmt.close();
}
if(null != conn) {
conn.close();
}
JDBC封裝:
既然我們瞭解了JDBC連接的過程,那麼我們大致可以將其分爲三個部分:sql業務操作前爲第一部分,準備部分,需要進行註冊和連接;sql業務操作爲第二部分;最後一部分爲關閉資源。那麼除第二部分sql業務操作外,其餘兩部分可以封裝在一個工具類中。
第一部分,準備:
註冊驅動可以放在靜態塊中,進行類的初始化:
static {
try {
Class.forName(DRIVERPATH);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
獲取Connection對象:
public static Connection getConn() {
synchronized(DBconUtil.class) {
try {
if(null == conn || conn.isClosed()) {
synchronized(DBconUtil.class) {
conn = DriverManager.getConnection(URL,USER,PASSWORD);
}
}
}catch(SQLException e) {
e.printStackTrace();
}
}
return conn;
}
關閉資源:
用stmt可以把pstmt的位置填null,用pstmt可以把stmt的位置填null。
public static void close(Connection con,PreparedStatement pstmt,Statement stmt,ResultSet rs) {
try {
if(null != rs) {
rs.close();
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
try {
if(null != stmt) {
stmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
try {
if(null != pstmt) {
pstmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
//關閉資源
try {
if(null != conn) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
}
DBconUtil類:
最後附上封裝類,整理不易。
package JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBconUtil {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/book";
private static final String USER = "root";
private static final String PASSWORD = "123456";
private static final String DRIVERPATH = "com.mysql.jdbc.Driver";
private static Connection conn = null;
Statement stmt = null;
static {
try {
Class.forName(DRIVERPATH);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() {
synchronized(DBconUtil.class) {
try {
if(null == conn || conn.isClosed()) {
synchronized(DBconUtil.class) {
conn = DriverManager.getConnection(URL,USER,PASSWORD);
}
}
}catch(SQLException e) {
e.printStackTrace();
}
}
return conn;
}
public static void close(Connection con,PreparedStatement pstmt,Statement stmt,ResultSet rs) {
try {
if(null != rs) {
rs.close();
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
try {
if(null != stmt) {
stmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
try {
if(null != pstmt) {
pstmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
//關閉資源
try {
if(null != conn) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
}
}