PreparedStatement

DB/JDBC : 2007. 4. 4. 20:00
반응형


import java.sql.*;

public class jdbctest {
  String driver_name;
  String URL;
  String uid;
     String passwd;
     String sql;

     Connection con;
     Statement stmt;
     ResultSet rs;
 
     public jdbctest() {
      //주의 : @//PC_이름:1521/orcl쓸것
   URL="jdbc:oracle:thin:@//localhost:1521/ORCL";
   uid="scott";
   passwd="tiger";
     }  
  public static void main(String[] args){
   jdbctest x = new jdbctest();
   x.DB_Connection();
   x.DB_DisConnection();
  }
  public void DB_Connection() {
         try{
    DriverManager.registerDriver(
      new oracle.jdbc.driver.OracleDriver());
    con = DriverManager.getConnection(URL,uid,passwd);
//    stmt = con.createStatement();
    sql="SELECT * FROM STUDENT where studno=?";
    PreparedStatement ps = con.prepareStatement(sql);    
//    rs=stmt.executeQuery("SELECT * FROM STUDENT");
    ps.setInt(1,10101);
    rs=ps.executeQuery();    
    while(rs.next()){
     System.out.println(rs.getString(1));
     System.out.println(rs.getString(2));
     System.out.println(rs.getString(3));
     System.out.println(rs.getString(4));
     System.out.println(rs.getString(5));
     System.out.println(rs.getString(6));
    }
   
    ps.setInt(1,20101);
    rs=ps.executeQuery();    
    while(rs.next()){
     System.out.println(rs.getString(1));
     System.out.println(rs.getString(2));
     System.out.println(rs.getString(3));
     System.out.println(rs.getString(4));
     System.out.println(rs.getString(5));
     System.out.println(rs.getString(6));
    }
         }catch(SQLException e)
         {
             e.printStackTrace();
         }
  }
  public void DB_DisConnection(){
   try{
    con.close();
   }catch(SQLException e){
    e.printStackTrace();
   }
  } 
}


/*-------------실행결과-------------/
10101
전인하
jun123
4
7907021369824
1979-07-02 00:00:00.0
20101
이동훈
Dals
1
8312101128467
1983-12-10 00:00:00.0
//-----------------------------------*/

반응형

'DB > JDBC' 카테고리의 다른 글

MetaData  (0) 2007.04.04
JDBC 기초  (0) 2007.04.03
JDBC란..?  (0) 2007.03.29
Posted by Real_G