import java.sql.*; //引入相应的包
public class TestPrepStmt {
public static void main(String[] args) {
if(args.length != 3){ //dept这张表有3个量,所以需要判断
System.out.println("error!");
System.exit(-1);
}
int deptno = 0;
try{
deptno = Integer.parseInt(args[0]); //键入第一个值存放在deptno里面,这需要try/catch
}catch (NumberFormatException e){
System.out.println("numberformat error!");
System.exit(-1);
}
String dname = args[1];
String loc = args[2];
PreparedStatement pstmt = null; //这里使用preparedstatement,好处大大滴~
Connection conn = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
//new oracle.jdbc.driver.OracleDriver();这是另外一个实例化的方法,个人不推荐
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","yangjiong","yangjiong");//链接oracle,我登上的是自己测试的账号
pstmt = conn.prepareStatement("insert into dept values(?,?,?)"); //使用?占位符 //,不想修改Scott账号
pstmt.setInt(1, deptno); //第一个?处为deptno的值
pstmt.setString(2, dname);
pstmt.setString(3,loc);
pstmt.executeUpdate();
}catch (ClassNotFoundException e){ //下面是exception的捕获
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}finally{
try{
if(pstmt != null){
pstmt.close();
pstmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
public class TestPrepStmt {
public static void main(String[] args) {
if(args.length != 3){ //dept这张表有3个量,所以需要判断
System.out.println("error!");
System.exit(-1);
}
int deptno = 0;
try{
deptno = Integer.parseInt(args[0]); //键入第一个值存放在deptno里面,这需要try/catch
}catch (NumberFormatException e){
System.out.println("numberformat error!");
System.exit(-1);
}
String dname = args[1];
String loc = args[2];
PreparedStatement pstmt = null; //这里使用preparedstatement,好处大大滴~
Connection conn = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
//new oracle.jdbc.driver.OracleDriver();这是另外一个实例化的方法,个人不推荐
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","yangjiong","yangjiong");//链接oracle,我登上的是自己测试的账号
pstmt = conn.prepareStatement("insert into dept values(?,?,?)"); //使用?占位符 //,不想修改Scott账号
pstmt.setInt(1, deptno); //第一个?处为deptno的值
pstmt.setString(2, dname);
pstmt.setString(3,loc);
pstmt.executeUpdate();
}catch (ClassNotFoundException e){ //下面是exception的捕获
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}finally{
try{
if(pstmt != null){
pstmt.close();
pstmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}