|  
  Connection con=this.creatCon();//獲得連接 
   PreparedStatement pstm=null;//命令器 
String sql="insert into student(student_id,name,sex,age) values(?,?,?,?)";//設(shè)置要執(zhí)行的SQL語句 
    pstm=con.prepareStatement(sql);//準(zhǔn)備執(zhí)行SQL語句命令- 
  
    pstm.setString(2, name);//設(shè)置第2個問號的值 
    pstm.setString(3, sex);//設(shè)置第3個問號的值 
    pstm.setInt(4, age);//設(shè)置第4個問號的值 
    pstm.setInt(1,student_id);//設(shè)置第1個問號的值 
    //pstm.executeQuery();//執(zhí)行SQL命令,這個命令是在查詢的時候,返回數(shù)據(jù)集ResultSet 
    pstm.executeUpdate();//執(zhí)行SQL命令,當(dāng)是插入,刪除,修改的時候用 
    System.out.println("寫入成功");//執(zhí)行后寫出是否成功 
連接的打開和關(guān)閉: 
// 創(chuàng)建一個連接Oracle數(shù)據(jù)庫的類 
public static Connection creatCon()  
{ 
   String username="scott"; 
   String password="tiger"; 
   Connection con=null; 
   try 
   { 
    Class.forName("Oracle.jdbc.driver.OracleDriver"); 
    String url="java:Oracle:thin:@localhost:1521:ora9i";//連接Oracle數(shù)據(jù)庫 
     /*class.forName("com.micrsoft.jdbc.sqlserver.SQLServerDriver"); 
     String url="java:microsoft:sqlserver://localhost:1433";//連接SQL server數(shù)據(jù)庫*/ 
 
    con=(Connection)DriverManager.getConnection(url,username,password); 
    return con; 
   } 
   catch(Exception exc) 
   { 
    System.out.println(exc); 
   } 
   return null; 
} 
//關(guān)閉連接 
public void closeCon(Connection c) 
{ 
   try 
   { 
    c.close(); 
   } 
   catch(Exception c1) 
   { 
    System.out.println(c1); 
   } 
} 
 
 
  本文出自:億恩科技【www.cmtents.com】 
      
      
		服務(wù)器租用/服務(wù)器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM] 
       |