本示例演示如何向数据库中插入1000条记录。
package c1;
import java.sql.*;
import java.util.Properties;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Test t=new Test();
//select * from dba_tablespaces
try{
DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver());
Properties conProps = new Properties();
conProps.put("user", "sys");
conProps.put("password", "test");
conProps.put("defaultRowPrefetch", "15");
conProps.put("internal_logon", "sysdba");
// Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test","system","test");
Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test",conProps);
PreparedStatement pstmt;
ResultSet rset;
//boolean tableexist;
pstmt = conn.prepareStatement("select count(*) from user_tables where table_name='PERSONS'");
//pstmt = conn.prepareStatement("select count(*) from dba_tablespaces");
pstmt.execute();//
rset = pstmt.executeQuery();
while (rset.next()){
System.out.println (rset.getString(1)); // Print col 1
if(rset.getInt(1)==1){
pstmt = conn.prepareStatement("DROP TABLE Persons");
pstmt.executeQuery();
}
}
pstmt = conn.prepareStatement("CREATE TABLE Persons(Id_P int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255))");
pstmt.execute();
int i;
for(i=0;i<1000;i++){
pstmt = conn.prepareStatement("INSERT INTO Persons VALUES ("+i+", 'Gates"+i+"', 'Bill', 'Xuanwumen 10', 'Beijing')");
pstmt.executeUpdate();
pstmt.close();//we need to close pstmt, otherwise there might be exceptions like java.sql.SQLException: ORA-01000: maximum open cursors exceeded
}
System.out.println(i+" records were inserted;");
}
catch(SQLException e){e.printStackTrace();}
}
}
阅读(661) | 评论(0) | 转发(0) |