?? 4.24.txt
字號:
一.用java語句通過jdbc操作數(shù)據(jù)庫,實(shí)現(xiàn)數(shù)據(jù)庫的增,刪,改,查
conn 文件夾
package conn;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConn {
public static Connection getDBConn()
{
Connection conn=null;
try {
/*載入驅(qū)動程序*/
Class.forName("oracle.jdbc.driver.OracleDriver");
/*用DriverManager.getConnection()方法連接數(shù)據(jù)庫,有三個參數(shù)*/
conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:dbl","scott","tiger");
} catch (Exception e) {
// TODO 自動生成 catch 塊
System.out.println("數(shù)據(jù)庫連接失敗");
e.printStackTrace();
}
return conn;
}
}
------------------------------------------------------------
vo 文件夾
package vo;
public class StudentVo {
private int id;
private String name;
private int age;
private String depart;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--------------------------------------------------
dao 文件夾
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import vo.StudentVo;
import conn.DBConn;
public class StudentDao {
***************
/*查詢的是一條數(shù)據(jù),用StudentVo就可以*/
public StudentVo selectById(int i)
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
StudentVo sto=null;
String sql="select studentid,name,age,department from student where studentid="+i+" ";
//System.out.println(sql);
try {
/*調(diào)用類DBConn中的方法,連接數(shù)據(jù)庫*/
conn=DBConn.getDBConn();
/*創(chuàng)建一個 Statement 對象來將 SQL 語句發(fā)送到數(shù)據(jù)庫。*/
st=conn.createStatement();
/* 執(zhí)行給定的 SQL 語句,該語句返回單個 ResultSet 對象。*/
rs=st.executeQuery(sql);
/*因?yàn)橥ㄟ^主鍵來查詢數(shù)據(jù),數(shù)據(jù)是唯一的,只執(zhí)行一次,所以用if語句*/
if(rs.next()) //next()方法是取出下一個的值
{
sto=new StudentVo();
sto.setId(rs.getInt("studentid"));
sto.setName(rs.getString("name"));
sto.setAge(rs.getInt("age"));
sto.setDepart(rs.getString("department"));
}
} catch (SQLException e) {
// TODO 自動生成 catch 塊
System.out.println("查詢失敗");
e.printStackTrace();
}finally{
/*最后不要忘了關(guān)閉資源和數(shù)據(jù)庫*/
try {
rs.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {
st.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
return sto;
}
********************
/*查詢的可能是多條數(shù)據(jù),用List作為返回值*/
public List selectByDept(String dept)
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
List l1=new ArrayList();
String sql="select studentid,name,age,department from student where department= '"
+dept+"'";
try {
conn=DBConn.getDBConn();
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next())
{
StudentVo svo=new StudentVo();
svo.setId(rs.getInt("studentid"));
svo.setName(rs.getString("name"));
svo.setAge(rs.getInt("age"));
svo.setDepart(rs.getString("department"));
l1.add(svo);
}
} catch (SQLException e) {
// TODO 自動生成 catch 塊
System.out.println("查詢失敗");
e.printStackTrace();
}finally{
try {if(rs!=null){
rs.close();
}
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {
if(st!=null){st.close();};
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {
if(true){conn.close();}
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
return l1;
}
**************************************
/*此方法的返回值是整型的,通過返回的值來判斷刪除數(shù)據(jù)是否成功*/
public int DlelteDemo(int i)
{
Connection conn=null;
Statement st=null;
int flg=0;
String sql="delete student where studentid="+i+" ";
System.out.println(sql);
try {
conn=DBConn.getDBConn();
/*將提交設(shè)置成手動提交,如果此處設(shè)成自動,在刪除數(shù)據(jù)的時候,可能出現(xiàn)異常,造成有的數(shù)據(jù)沒被刪掉*/
conn.setAutoCommit(false);
/*創(chuàng)建一個 Statement 對象來將 SQL 語句發(fā)送到數(shù)據(jù)庫。*/
st=conn.createStatement();
/*執(zhí)行給定 SQL 語句,該語句可能為 INSERT、UPDATE 或 DELETE 語句,或者不返回任何內(nèi)容的 SQL 語句(如 SQL DDL 語句)。*/
flg=st.executeUpdate(sql);
/*手動提交以上的操作*/
conn.commit();
} catch (SQLException e) {
try {
/*出現(xiàn)異常時,恢復(fù)成操作前的樣子*/
conn.rollback();
} catch (SQLException e1) {
// TODO 自動生成 catch 塊
e1.printStackTrace();
}
// TODO 自動生成 catch 塊
System.out.println("刪除失敗");
e.printStackTrace();
}finally{
try {
/*最后不要忘記設(shè)成自動提交的模式和關(guān)閉資源的數(shù)據(jù)庫*/
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {
st.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
return flg;
}
****************************************
/*此方法的返回值是整型的,通過返回的值來判斷插入數(shù)據(jù)是否成功*/
public int InsertDemo(StudentVo s)
{
Connection conn=null;
PreparedStatement pst=null;
int flg=0;
String sql="insert into student (studentid,name,age,department)"+
"values(?,?,?,?)";
try {
conn=DBConn.getDBConn();
conn.setAutoCommit(false);
/* 創(chuàng)建一個 PreparedStatement 對象來將參數(shù)化的 SQL 語句發(fā)送到數(shù)據(jù)庫。*/
pst=conn.prepareStatement(sql);
pst.setInt(1, s.getId());
pst.setString(2, s.getName());
pst.setInt(3, s.getAge());
pst.setString(4, s.getDepart());
flg=pst.executeUpdate();
conn.commit();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
try {
conn.rollback();
} catch (SQLException e1) {
// TODO 自動生成 catch 塊
e1.printStackTrace();
}
System.out.println("插入失敗");
e.printStackTrace();
}finally{
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {
pst.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
return flg;
}
**********************************************
public int UpdateDemo(int i,StudentVo s)
{
Connection conn=null;
PreparedStatement pst=null;
int flg=0;
String sql="update student set studentid=?,name=?,age=?,department=? "+
"where studentid="+i+" ";
try {
conn=DBConn.getDBConn();
conn.setAutoCommit(false);
//創(chuàng)建一個 PreparedStatement 對象來將參數(shù)化的 SQL 語句發(fā)送到數(shù)據(jù)庫。
pst=conn.prepareStatement(sql);
/*將指定參數(shù)設(shè)置為給定 Java int 值。在將該對象發(fā)送到數(shù)據(jù)庫時,驅(qū)動程序?qū)⑺D(zhuǎn)換成一個 SQL INTEGER 值。 */
pst.setInt(1, s.getId());
pst.setString(2, s.getName());
pst.setInt(3, s.getAge());
pst.setString(4, s.getDepart());
flg=pst.executeUpdate();
conn.commit();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
try {
conn.rollback();
} catch (SQLException e1) {
// TODO 自動生成 catch 塊
e1.printStackTrace();
}
System.out.println("更新失敗");
e.printStackTrace();
}finally{
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
if(pst!=null){try {
pst.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}}
if(conn!=null){try {
conn.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}}
}
return flg;
}
}
-------------------------------------------------------------------
bo 文件夾
package bo;
import java.util.List;
import vo.StudentVo;
import dao.StudentDao;
public class Business {
/*測試查詢一條數(shù)據(jù)*/
public void displaySelectById(int i)
{
StudentDao sdao=new StudentDao();
StudentVo svo=sdao.selectById(i);
if (svo!=null)
{
System.out.println(svo.getId()+" "+svo.getName()+" "+svo.getAge()+" "+svo.getDepart());
}
}
/*測試查詢多條數(shù)據(jù)*/
public void displaySelectByDept(String dept)
{
StudentDao sdo=new StudentDao();
List l1=sdo.selectByDept(dept);
if(l1!=null)
{
for(int i=0;i<l1.size();i++)
{
StudentVo svo=(StudentVo)l1.get(i);
if(svo!=null){
System.out.println(svo.getId()+" "+svo.getName()+" "+svo.getAge()+" "+svo.getDepart());
}
}
}
}
/*測試刪除數(shù)據(jù)*/
public void displayDelete(int i)
{
StudentDao sdo=new StudentDao();
int flg=sdo.DlelteDemo(i);
if(flg!=0){
System.out.println("刪除成功");
}else{
System.out.println("刪除失敗");
}
}
/*測試插入數(shù)據(jù)*/
public void displayInsert(StudentVo s)
{
StudentDao sdo=new StudentDao();
int flg=sdo.InsertDemo(s);
if (flg!=0){
System.out.println("插入成功");
}else{
System.out.println("插入失敗");
}
}
/*測試更新一條數(shù)據(jù)*/
public void displayUpdate(int i,StudentVo s)
{
StudentDao sdo=new StudentDao();
int flg=sdo.UpdateDemo(i, s);
if(flg!=0){
System.out.println("更新成功");
}else{
System.out.println("更新失敗");
}
}
}
------------------------------------------
test 文件夾
package test;
import vo.StudentVo;
import bo.Business;
public class Test {
public static void main(String [] args)
{
Business b1=new Business();
b1.displaySelectById(1);
//b1.displaySelectByDept("計(jì)算機(jī)");
//b1.displayDelete(6);
StudentVo svo=new StudentVo();
svo.setId(3);
svo.setName("小傳");
svo.setAge(24);
svo.setDepart("英語");
//b1.displayInsert(svo);
//b1.displayUpdate(3, svo);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -