JDBC串連ORACLE 增刪改查

來源:互聯網
上載者:User
package com.db.conn;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class ConOracl {


// 資料庫驅動類
private String dbDriver = "oracle.jdbc.driver.OracleDriver";
// 串連資料庫url
private String dbURL = "jdbc:oracle:thin:@localhost:1521:xe";
// 串連資料庫使用者名稱
private String dbUser = "system";
// 串連資料庫密碼
private String dbPwd = "123";
// 擷取資料庫連接方法, 返回Connection對象
private Connection con = null;
//資料執行語句
private Statement  stat = null;
private String sql=null;
private ResultSet rs=null;
//建立資料庫連接
public Connection getDBConnect() {
try {
// 載入資料庫驅動
Class.forName(dbDriver);
con = DriverManager.getConnection(dbURL, dbUser, dbPwd);
} catch (Exception e) {
System.out.println(e);
}
return con;
}
//增加
public void Add()
{
con=getDBConnect();
sql="insert into student(stuno,stuname,stusex)" +
"values('1','lucy','w')";
try {
stat=con.createStatement();
stat.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//刪除
public void Delete()
{
con=getDBConnect();
sql="delete  from student " +
"where stuno=1";
try {
stat=con.createStatement();
stat.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
//修改
public void Update()
{
con=getDBConnect();
sql="update student set stuname='ben' where stuno='2'";
try {
stat=con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
//查詢
public void Search() {
con = getDBConnect();
// 建立表的sql語句
sql = "SELECT * FROM student";
try {
stat = con.createStatement();
rs= stat.executeQuery(sql);
while (rs.next()) {
String stuno = rs.getString(1);
String stuname = rs.getString(2);
String stusex = rs.getString(3);
System.out.println(stuno + "," + stuname + "," + stusex);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//功能:關閉資料庫的串連
 
    public void close() {//6.釋放資源
      try { // 捕捉異常
           try {
               if (rs!= null) { // 當ResultSet對象的執行個體rs不為空白時
                rs.close(); // 關閉ResultSet對象
              }
            } finally {
              try {
                    if (stat != null) { // 當Statement對象的執行個體stmt不為空白時
                    stat.close(); // 關閉Statement對象
                    }
              } finally {
                   if (con!= null) { // 當Connection對象的執行個體conn不為空白時
                        con.close(); // 關閉Connection對象
                }
              }
         }
       } catch (Exception e) {
           e.printStackTrace(System.err); // 輸出異常資訊
       }
   }
public static void main(String[] args) {
ConOracl oracl = new ConOracl();
System.out.println("操作之前:");
oracl.Search();
oracl.Add();
oracl.Delete();
oracl.Update();
System.out.println("操作之後:");
oracl.Search(); 
oracl.close();
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.