思維導圖 – Java通過JDBC驅動連接SQL server數據庫
一:文件結構
二:樣例代碼:
- BaseDao.java:
/*Auther: Jason
Time:2016-6-24
*/
package cs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import com.sun.corba.se.pept.transport.Connection;
/* 基本數據庫類 */
public class BaseDao {
/* 數據庫驅動 */
private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
/* 連接數據庫路徑 */
private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=MyDB";
/* 用戶信息 */
private static final String user = "sa";
private static final String password = "chenmeng";
/* 靜態塊執行加載數據庫驅動,只執行一次 */
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/*
* connect, PreparedStatement, ResultSet 對象聲明 PreparedStatement作用於操作符
*/
protected Connection conn;
protected PreparedStatement pstmt;
protected ResultSet rs;
/* 功能函數 */
/* 創建connection對象 */
protected void getConnection() {
if (conn == null) {
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/* 獲取PSTMT對象, SQL語句作爲傳參 */
protected void getPreparedStatement(String sql) {
if (conn == null) {
getConnection();
}
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
/* 沒有佔位符的SQL查詢 */
protected void getResultSet(String sql) {
if (pstmt == null) {
getPreparedStatement(sql);
}
try {
rs = pstmt.executeQuery();
} catch (SQLException e) {
}
}
/* 檢查關閉功能 */
protected void close() {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- Book.java
package cs;
public class Book {
/* 類私有屬性 */
private int id;
private String bookname;
private String author;
private String publisher;
private int price;
/* 默認構造函數 */
public Book() {
super();
}
public Book(String bookname, String author, int price) {
super();
this.bookname = bookname;
this.author = author;
this.price = price;
}
public Book(int id, String bookname, String author, int price) {
super();
this.id = id;
this.bookname = bookname;
this.author = author;
this.price = price;
}
/* 操作屬性方法 */
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookname;
}
public void setBookName(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
- BookDao,java
package cs;
import java.sql.SQLException;
import java.util.ArrayList;
public class BookDao extends BaseDao {
public ArrayList<Book> queryBooksByPublisher(String publisher) {
ArrayList<Book> bookList = new ArrayList<Book>();
String sql = "select * from tbook where publisher=?";
getPreparedStatement(sql);
try {
pstmt.setString(1, publisher);
rs = pstmt.executeQuery();
while (rs.next()) {
/* 循環讀取SQL數據 */
int id = rs.getInt("id");
String name = rs.getString("name");
String author = rs.getString("author");
int price = rs.getInt("price");
/* Book初始化 */
Book book = new Book(id, name, author, price);
bookList.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
/* 釋放資源 */
close();
}
return bookList;
}
}
- BookTest.java
package cs;
import java.util.ArrayList;
public class BookTest {
public static void main(String[] args) {
/* 創建books對象數組 */
ArrayList<Book> books = new BookDao()
.queryBooksByPublisher("四川師範大學出版社");
/* 迭代輸出books對象中的數據 */
for (Book book : books) {
System.out.println(book.getBookName() + book.getPrice());
}
}
}
三:數據庫表單
MyDB:
create table tbook(
id int primary key,
bookname varchar(20),
author varchar(20),
publisher varchar(50),
price int
)
四:環境
1)平臺:Win10+ MyEclipse10+ 內置Tomcat6.0服務器;
2)JRE:Java1.8.0環境
3)JDBC驅動:sqljdbc4.jar
4)數據庫:SQLserver 2012
五:其他
1)有相關問題可以聯繫博主,郵箱:[email protected] 歡迎打擾;
2)下片博文預告:servlet + ThreadLocal解決多線程併發問題;
3)今後會新增changelog修改日誌;
4)如果有更好的解決方法,請fork;
5)參考資料:
http://blog.csdn.net/stewen_001/article/details/19553173/
http://blog.csdn.net/keenweiwei/article/details/7332261
http://blog.csdn.net/that3/article/details/7584454
http://blog.csdn.net/chenleixing/article/details/44024095
6)版權所有,不得轉載!