JavaSE绑定oracle数据库应用的增、删、改、查实例

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

public class Demo {
 /**
  * Driver用法:
  * Driver driver = new oracle.jdbc.driver.OracleDriver();
  * DriverManager.registerDriver(driver);
  *
  * Class.forName("oracle.jdbc.driver.OracleDriver");
  *
  * Connection用法:
  * Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle","system","oracle");
  *
  * Statement语句用法:
  * Statement stmt = conn.createStatement();
  * int i = stmt.executeUpdate(sql);
  *
  * PreparedStatement语句用法:
  * PreparedStatement pstmt = conn.prepareStatement(sql);
  * pstmt.setString(1,"");
  * pstmt.setString(2,"");
  * int i = pstmt.executeUpdate();
  *
  * ResultSet用法:
  * ResultSet rs = stmt.executeQuery(sql);
  * while(rs.next()){
  * System.out.print(rs.getString(1)+"\t");
  * System.out.print(rs.getString("price")+"\t");
  * }
  * 
  */
 /*新增
 public static void add(){
  Connection conn = null;
  Statement stmt = null;
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   //Driver driver = new oracle.jdbc.driver.OracleDriver();
   //DriverManager.registerDriver(driver);
   conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "system", "oracle");
   stmt = conn.createStatement();
   String sql = "insert into My_Price values(16,12.5,'脊骨')";
   int i = stmt.executeUpdate(sql);
   if(i>0){
    System.out.println("数据新增成功");
   }
   conn.close();
  }catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    if(stmt!=null){
     stmt.close();
    }if(conn!=null){
     conn.close();
    }
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }*/
 /*查询
 public static void queryAll(){
  Connection conn = null;
  Statement stmt = null;
  ResultSet rs = null;
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   //Driver driver = new oracle.jdbc.driver.OracleDriver();
   //DriverManager.registerDriver(driver);
   conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "system", "oracle");
   stmt = conn.createStatement();
   String sql = "select price_id,price,price_name from My_Price";
   rs= stmt.executeQuery(sql);
   while(rs.next()){
    System.out.print(rs.getString(1)+"\t");
    System.out.print(rs.getString("price")+"\t");
    System.out.println(rs.getString("price_name"));
   }
  }catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    if(rs!=null){
     rs.close();
    }if(stmt!=null){
     stmt.close();
    }if(conn!=null){
     conn.close();
    }
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 */
 /*修改
 public static void update(){
  Connection conn = null;
  PreparedStatement pstmt = null;
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   //Driver driver = new oracle.jdbc.driver.OracleDriver();
   //DriverManager.registerDriver(driver);
   conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "system", "oracle");
   
   String sql = "update prefix_product set category=? where productid=?";
   pstmt=conn.prepareStatement(sql);
   pstmt.setString(1,"未知");
   pstmt.setString(2,"M08");
   int i=pstmt.executeUpdate();
   if(i>0){
    System.out.println("数据更新成功");
   }
   conn.close();
  }catch (Exception e) {
   e.printStackTrace();
  }finally{
   try {
    if(pstmt!=null){
     pstmt.close();
    }if(conn!=null){
     conn.close();
    }
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 */
 /*删除
 public static void delete(){
  Connection conn = null;
  PreparedStatement pstmt = null;
  try {
   //oracle.jdbc.driver.OracleDriver
   //jdbc:oracle:thin:@localhost:1521:oracle
   Class.forName("oracle.jdbc.driver.OracleDriver");   
   conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "system", "oracle");
      pstmt= conn.prepareStatement("delete from prefix_product where productid = ?");
      pstmt.setString(1, "M09");
      int i = pstmt.executeUpdate();
      if(i>0){
       System.out.println("受影响的行数为"+i);
      }
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    pstmt.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
   try {
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
 */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //update();
  //delete();

 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值