一个JAVA调用ORACLE和MYSQL返回结果集的代码,给大家分享一下。
import java.sql.*;
import java.io.*;
//import oracle.jdbc.driver.*;
public class JDBCDemo {
//private String driver = "oracle.jdbc.driver.OracleDriver";
//private String url = "jdbc:oracle:thin:@192.168.1.213:1521:luke";
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://192.168.1.213:3306/luke";
private Connection conn = null;
public JDBCDemo()
throws ClassNotFoundException, SQLException {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, "luke", "luke");
}
catch (SQLException ex) {
System.out.println("Failed to establish a connection to: " + url);
throw ex;
}
}
private void execute(int n)
throws SQLException {
String query = "{call proc_shop_num(?,?,?) }";
System.out.println("Query: " + query + "n");
CallableStatement stmt = conn.prepareCall(query);
stmt.setInt(1,n);
stmt.setInt(2,3);
//stmt.registerOutParameter(1, OracleTypes.CURSOR);
//stmt.execute();
ResultSet rs = stmt.executeQuery();
//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 cleanup() throws SQLException {
if (conn != null)
conn.close();
}
public static void main(String[] args) throws Exception {
JDBCDemo t = new JDBCDemo();
try {
t.execute(Integer.parseInt(args[0]));
t.cleanup();
}
catch (SQLException ex) {
System.out.println("Demo failed: " + ex.getMessage());
}
}
}