package com.test1;
import java.sql.*;
public class test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
PreparedStatement ps1=null;
PreparedStatement ps2=null;
ResultSet rs=null;
Connection ct=null;
try{
//1、加载驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//2、得到连接
ct=DriverManager.getConnection("jdbc:odbc:mytest","sa","*******");
//3、创建PreparedStatement
ps1=ct.prepareStatement("insert into dept values(?,?,?)");
ps1.setInt(1, 80);
ps1.setString(2, "工商部");
ps1.setString(3, "武汉");
int i=ps1.executeUpdate();
if(i==1){
System.out.println("ok");
}else if(i==0){
System.out.println("error");
}
ps2=ct.prepareStatement("select *from dept where deptno>?");
ps2.setInt(1, 20);
rs=ps2.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
}catch(Exception e){
e.printStackTrace();
}finally{
//关闭资源
try {
if(rs!=null){
rs.close();
}
if(ps2!=null){
ps2.close();
}
if(ps1!=null){
ps1.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}