JAVA调用ORACLE和MYSQL返回结果集
导入Java . SQL . *;
导入Java . io . *;
//导入Oracle . JDBC . driver . *;
公共类JDBC demo {
//私有字符串driver = " Oracle . JDBC . driver . Oracle driver ";
//私有字符串URL = " JDBC:Oracle:thin:@ 192 . 168 . 1 . 213:1521:Luke ";
private String driver = " com . MySQL . JDBC . driver ";
私有字符串URL = " JDBC:MySQL://192 . 168 . 1 . 213:3306/Luke ";
专用连接连接= null
public JDBC demo()
throws ClassNotFoundException,SQLException {
try {
class . for name(driver);
conn = driver manager . get connection(URL," luke "," Luke ");
}
catch(SQLException ex){
system . out . println("未能建立到:"+ url)的连接);
扔ex;
}
}
private void execute(int n)
throws SQLException {
String query = " { call proc _ shop _ num(?,?,?) }";
system . out . println(" Query:"+Query+" n ");
callable statement stmt = conn . prepare call(query);
stmt.setInt(1,n);
stmt.setInt(2,3);
//stmt . registeroutparameter(1,OracleTypes。光标);
//stmt . execute();
ResultSet RS = stmt . execute query();
//ResultSet RS =(ResultSet)stmt . getobject(1);
system . out . println(stmt . getint(1));
while(RS . next()){
system . out . println(RS . getstring(2));
}
RS . close();
stmt . close();
}
private void clean up()抛出SQLException {
if (conn!= null)
conn . close();
}
公共静态void main(String[] args)引发异常{
JDBC demo t = new JDBC demo();
try {
t . execute(integer . parse int(args[0]);
t . clean up();
}
catch(SQLException ex){
system . out . println("演示失败:"+ex . getmessage());
}
}
}
0条评论