數據庫的建立應該不用再講了吧,現在講使用JAVA與數據庫進(jìn)行交互
使用JDBC進(jìn)行數據庫的增刪改查操作
1.下載Microsoft SQL Server 2005 JDBC 驅動(dòng)包jar文件
將jar文件引入工程中
2.封裝數據庫鏈接的獲取和關(guān)閉操作
import java.sql.*;
public class BaseDao {
/**
* 數據庫驅動(dòng)類(lèi)的字符串,完整的包名加類(lèi)名 在工程中查看添加的jar文件 能看到這個(gè)類(lèi)
*/
private static final String DRIVE = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
/**
* 數據庫連接地址
*
* DataBaseName=數據庫名稱(chēng) 其它固定
*/
private static final String URL = "jdbc:sqlserver://localhost:1433;DataBaseName=bbs";
/**
* 連接數據庫的用戶(hù)名
*/
private static final String USER = "sa";
/**
* 用戶(hù)密碼
*/
private static final String PASSWORD = "";
/**
* 獲取連接 異常直接拋出 或者捕獲后自定義異常信息再拋出
*/
public static Connection getConnection() throws Exception {
Class.forName(DRIVE);
return DriverManager.getConnection(URL, USER, PASSWORD);
}
/**
* 關(guān)閉與數據庫的連接 釋放資源
*/
public static void closeAll(ResultSet resultSet, PreparedStatement pst,
Connection connection) throws Exception {
if (resultSet != null)
resultSet.close();
if (pst != null)
pst.close();
if (connection != null)
connection.close();
}
}
3.創(chuàng )建圖書(shū)的實(shí)體類(lèi)
public class Book {
/**
* 數據庫主鍵
*/
private Long id;
/**
* 作者
*/
private String author;
/**
* 書(shū)名
*/
private String name;
/**
* 默認構造
*
*/
public Book() {
}
/**
* 全字段構造
* @param id
* @param author
* @param name
*/
public Book(Long id, String author, String name) {
this.id = id;
this.author = author;
this.name = name;
}
/**
* 以下為讀寫(xiě)屬性的方法
* @return
*/
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.創(chuàng )建與圖書(shū)表交互的工具類(lèi)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class BookDao {
/**
* 添加新書(shū)
*
* @param book 要添加入數據庫的圖書(shū) 作者 書(shū)名 必須給定
*/
public void addBook(Book book) throws Exception {
// 連接
Connection connection = null;
// 執行語(yǔ)句
PreparedStatement pst = null;
try {
connection = BaseDao.getConnection();
// 構造執行語(yǔ)句
String sql = "insert into book values(" + book.getAuthor() + ","
+ book.getName() + ")";
pst = connection.prepareStatement(sql);
pst.executeUpdate();
} catch (Exception e) {
// 拋出異常
throw e;
} finally {
// 無(wú)論是否異常 均關(guān)閉數據庫
BaseDao.closeAll(null, pst, connection);
}
}
/**
* 查詢(xún)所有書(shū)籍列表
*/
public List<Book> getBooks() throws Exception {
// 用于存放查尋結果的集合
List<Book> books = new ArrayList<Book>();
// 連接
Connection connection = null;
// 執行語(yǔ)句
PreparedStatement pst = null;
// 查詢(xún)結果
ResultSet resultSet = null;
try {
connection = BaseDao.getConnection();
// 構造查詢(xún)語(yǔ)句
String sql = "select * from book";
pst = connection.prepareStatement(sql);
resultSet = pst.executeQuery();
// 循環(huán)讀取查詢(xún)結果行
while (resultSet.next()) {
// getXXX的參數為數據表列名
Book book = new Book(resultSet.getLong("id"), resultSet
.getString("author"), resultSet.getString("name"));
// 將封裝好的圖書(shū)對象存入集合
books.add(book);
}
} catch (Exception e) {
// 拋出異常
throw e;
} finally {
// 無(wú)論是否異常 均關(guān)閉數據庫
BaseDao.closeAll(resultSet, pst, connection);
}
// 返回查詢(xún)結果
return books;
}
/**
*其它方法類(lèi)似上面 只是語(yǔ)句不同
*/
}
當然 以上只是簡(jiǎn)單的封裝 初學(xué)者可以在理解以上代碼的基礎上 進(jìn)行更高級的封裝
5.使用BookDao添加書(shū)籍和獲取所有書(shū)籍列表
import java.util.List;
/**
* 測試類(lèi)
* @author Administrator
*
*/
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//創(chuàng )建工具類(lèi)對象
BookDao dao = new BookDao();
//創(chuàng )建一本圖書(shū)
Book book = new Book(null,"QQ:495691293","編程菜鳥(niǎo)");
//添加書(shū)籍到數據庫
dao.addBook(book);
//獲取所有圖書(shū)列表
List<Book> books = dao.getBooks();
//輸出結果
for (Book b : books) {
System.out.println(b.getId()+"\t"+b.getAuthor()+"\t"+b.getName());
}
}
}
聯(lián)系客服