public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Class.forName("oracle.jdbc.driver.OracleDriver");//引用Oracle数据库连接驱动包
Connection con =DriverManager.getConnection("jdbc:oracle:thin:@172.18.2.248:1521:leoorcl","bruce","bruce");
//preparedStatement 是预编译sql
PreparedStatement ps = con.prepareStatement("select * from userr where money>? and name>?");//?叫占位符
//此处有多少 ? 后面就必要有多少setInt(index,值) index从1开始 从左到右
ps.setInt(1,30000);//设置占位符的值 从左到右
ps.setInt(2,2);
ResultSet rs = ps.executeQuery();
while(rs.next()){ //从结果中取数据
System.out.println("money:"+rs.getString("money"));//通过属性名取
System.out.println("name:"+rs.getString(1));//通过列号取
}
if(rs == null){//用完过后必须关闭 从子到父的顺序关闭
rs.close();
}
if(ps == null){
ps.close();
}
if(con == null){
con.close();
}
}
/*
//executeUpdate() 用法
// 第一个参数不变,第二个参数从0取到9
ps.setString(1, "name");
for (int i = 0; i < 10; i++) {
ps.setInt(2, i);
int rowCount = ps.executeUpdate();//executeUpdate() 返回是一个int 变量
}
*/