【JDBC】java PreparedStatement操作oracle数据库

************************************************************************ ****原文:blog.csdn.net/clark_xu  徐长亮的专栏 ************************************************************************
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.Statement;
import java.sql.PreparedStatement;
public class lx02{

 public static void main(String[] args) throws SQLException, ClassNotFoundException{
  // 加载驱动
  Class.forName("oracle.jdbc.driver.OracleDriver");
  java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  
  //建立连接
  String url = "jdbc:oracle:thin:@100.100.100.20:1521:dbsid2";
  String user="scott";
  String password="tiger";
  Connection con=java.sql.DriverManager.getConnection(url, user, password);
  
  //执行语句
/*  String sql1="select * from user_tables";
  Statement smt=con.createStatement();
        ResultSet rs1=smt.executeQuery(sql1);
*/     
     //preparestatemetn语句: 输入
 /*    String sql2="insert into a(deptno,dname,loc) values(?,?,?)";
  PreparedStatement ps=con.prepareStatement(sql2);
     ps.setString(1,"50");
     ps.setString(2, "pmo");
     ps.setString(3,"beijing");
     int rs2=ps.executeUpdate();
*/      
        //查询
  String sql="select dname from a where dname like ?";
  PreparedStatement ps=con.prepareStatement(sql);
     ps.setString(1, "pmo");
     ResultSet rs=ps.executeQuery();
 
  
  //删除字节
/*  String sql="delete from a where deptno=50";
  PreparedStatement ps=con.prepareStatement(sql);
  ps.executeUpdate();
*/
  
  //显示结果
        while(rs.next()){
         System.out.println(rs.getString(1)); 
//      System.out.println(rs.getString(1) + "  " + rs.getString(2)+"   "+ rs.getString(3));
    }


       //释放
       rs.close();
       ps.close();
       con.close();
  
 }

}

你可能感兴趣的:(【JDBC】java PreparedStatement操作oracle数据库)