版權聲明:尊重部落客原創文章,轉載請注明出處哦~http://blog.csdn.net/eson_15/article/details/51308449
程式設計從user表中讀取資料,并列印在控制台視窗中。
create database test character set utf8 collate utf8_general_ci;
use test
create table user
(
id int primary key,
name varchar(20),
password varchar(20),
email varchar(20),
birthday date
);
insert into user(id,name,password,email,birthday) values(1,'zw','123','[email protected]','1990-09-19');
insert into user(id,name,password,email,birthday) values(2,'ls','123','[email protected]','1991-04-19');
insert into user(id,name,password,email,birthday) values(3,'ww','123','[email protected]','1992-06-19');
public class demo1 {
public static void main(string[] args) throws exception {
string url = "jdbc:mysql://localhost:3306/day14";
string username = "root";
string password = "root";
try{
//1.加載驅動
//drivermanager.registerdriver(new driver());
class.forname("com.mysql.jdbc.driver");
//2.擷取資料庫的連接配接
connection conn = drivermanager.getconnection(url, username, password);
//3.獲得用于向資料庫發送sql語句的statement對象
statement st = conn.createstatement();
//4.向資料庫發sql,并擷取代表結果集的resultset
string sql = "select id,name,password,email,birthday from user";
resultset rs = st.executequery(sql);
//取出結果集的資料
while(rs.next()){
int id = (integer) rs.getobject("id");
string name = (string)rs.getobject("name");
string pd = (string)rs.getobject("password");
string email = (string)rs.getobject("email");
date birthday = (date)rs.getobject("birthday");
system.out.println(id+","+name+","+pd+","+email+","+birthday);
}
}
//關閉連接配接
finally{
if(rs != null){
try{
rs.close();
}catch(exception e) {
e.printstacktrace();
}
rs = null;
if(st != null){
st.close();
st = null;
if(conn != null){
conn.close();
conn = null;
}
}
jdbc程式中的drivermanager用于加載驅動,并建立與資料庫的連接配接,這個類的常用方法有:
drivermanager.registerdriver(new driver());
drivermanager.getconnection(url, user, password);
注意:在實際開發中不推薦采用registerdriver方法注冊驅動,原因有二:
a. 檢視driver的源代碼可以看到,如果采用此種方式,會導緻驅動程式注冊兩次,也就是在記憶體中會有兩個driver對象;
b. 程式依賴mysql的api,脫離mysql的jar包,程式将無法編譯,将來程式切換底層資料庫将會非常麻煩。
推薦方式:class.forname("com.mysql.jdbc.driver");
采用此種方式不會導緻驅動對象在記憶體中重複出現,并且采用此種方式,程式僅僅隻需要一個字元串,不需要依賴具體的驅動,使程式的靈活性更高。同樣,在開發中也不建議采用具體的驅動類型指向getconnection方法傳回的connection對象。
jdbc程式中的connection對象用于代表資料庫的連接配接,connection是資料庫程式設計中最重要的一個對象,用戶端與資料庫所有互動都是通過connection對象完成的。這個對象常用的方法有:
createstatement(); //建立向資料庫發送sql的statement對象
preparestatement(sql); //建立向資料庫發送預編譯sql的preparestatement對象。這個更常用
preparecall(sql); //建立執行存儲過程中的callablestatement對象
setautocommit(boolean autocommit); //設定事物是否自動送出
commit(); //在連結上送出事物
rollback(); //在此連結上復原事物
jdbc程式中的statement對象用于向資料庫發送sql語句,statement對象常用方法有:
<span style="font-family:microsoft yahei;">executequery(string sql); //用于向資料庫發送查詢語句
executeupdate(string sql); //用于向資料庫發送insert,update或delete語句
execute(string sql); //用于向資料庫發送任意sql語句
addbatch(string sql); //把多條sql語句放到一個批進行中
executebath(); //向資料庫發送一批sql語句執行
</span>
jdbc程式中的resultset對象用于代表sql語句的執行結果。resultset封裝執行結果時,采用的類似于表格的方式。resultset對象維護了一個指向表格資料行的遊标,初始的時候,遊标在第一行之前,調用該對象的next()方法,可以使遊标指向具體的資料行,進行調用方法擷取該行的資料。由于resultset用于封裝執行結果,是以該對象提供的都是用于擷取資料的get方法:
<span style="font-family:microsoft yahei;">//擷取任意類型的資料
getobject(int index); //index表示列号
getobject(string columnname); //columnname表示列名,建議用這種方法,更好維護
//擷取指定類型的資料(int,string,date等)
getstring(int index);
getstring(string columnname);
resultset除了提供get方法以外,還提供了對結果集進行滾動的方法:
<span style="font-family:microsoft yahei;">next(); //移動到下一行
previous(); //移動到前一行
absolute(int row); //移動到指定行
beforefirst(); //移動到resultset的最前面
afterlast(); //移動到resultset的最後面
為了確定資源釋放代碼能運作,資源釋放代碼一定要寫在finally語句中。
jdbc技術主要是同資料庫打交道,那麼免不了增删改查,由上面的代碼可以看出,在對資料庫進行操作之前需要先建立連接配接,在操作之後都需要釋放資源,是以我們可以把這兩部分内容抽取出來,寫到jdbcutils類中來實作:
public class jdbcutils {
private static string driver = null;
private static string url = null;
private static string username = null;
private static string password = null;
//加載驅動
static{
try {
//db.properties是一個配置檔案,裡面有連接配接資料庫所需要的資訊
inputstream in = jdbcutils.class.getclassloader().getresourceasstream("db.properties");
properties prop = new properties();
prop.load(in);//加載配置檔案
driver = prop.getproperty("driver");
url = prop.getproperty("url");
username = prop.getproperty("username");
password = prop.getproperty("password");
class.forname(driver);//加載驅動
} catch (exception e) {
throw new exceptionininitializererror(e);
public static connection getconnection() throws sqlexception{
return drivermanager.getconnection(url, username, password);//獲得connection
public static void release(connection conn, statement st, resultset rs){ //釋放資源
if(rs != null){
try{
rs.close();
}catch(exception e) {
e.printstacktrace();
rs = null;
if(st != null){
st.close();
st = null;
if(conn != null){
conn.close();
conn = null;
}
db.properties檔案:
driver=com.mysql.jdbc.driver
url=jdbc:mysql://localhost:3306/day14
username=root
password=root
這樣我們就完成了資料庫的連接配接和資源的釋放工具類。下面我們開始編寫對資料庫的增删改查:
//使用jdbc對資料庫的增删改查
public class demo2 {
private connection conn = null;
private statement st = null;
private resultset rs = null;
@test
public void insert(){
conn = jdbcutils.getconnection();
st = conn.createstatement();
string sql = "insert into user(id,name,password,email,birthday) values(3,'ww','123','[email protected]','1982-06-14');";
int num = st.executeupdate(sql);//傳回的是該sql語句會影響資料庫的幾行
if(num > 0){
system.out.println(num);
system.out.println("插入成功");
e.printstacktrace();
}finally {
jdbcutils.release(conn, st, rs);
public void delete(){
string sql = "delete from user where password='123'";
int num = st.executeupdate(sql);
system.out.println("删除成功");
public void update(){
string sql = "update user set password='456' where name='ww'";
if(num > 0) {
system.out.println("修改成功");
public void find(){
rs = st.executequery(sql);
int id = (integer)rs.getobject("id");
string password = (string)rs.getobject("password");
system.out.println(id+","+name+","+password+","+email+","+birthday);
jdbc基本知識點就介紹到這,後面再介紹一些進階點的應用,如有錯誤之處,歡迎留言指正~
_____________________________________________________________________________________________________________________________________________________
-----樂于分享,共同進步!