Import java.sql.*;
/**
* <P>TITLE:JDBC Connection Database </p>
* <p>description: This example demonstrates how to use JDBC to connect to an Oracle database and to demonstrate adding data and querying data. </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:JDBCSTMConn.java</p>
* @version 1.0
*/
public class jdbcstmconn{
private static String url= "";
private static String username= "";
private static String password= "";
/**
*<br> Method Description: Obtain data connection
*<br> input Parameters:
*<br> return Type: Connection Connection object
*/
public Connection Conn () {
try {
//load JDBC driver
class.forname ("Oracle.jdbc.driver.OracleDriver");
//CREATE DATABASE connection
Connection con = drivermanager.getconnection (URL, username, password);
return con;
}catch (classnotfoundexception cnf) {
System.out.println ("Driver not find:" +cnf);
return null;
}catch (SQLException sqle) {
System.out.println ("Can´t Connection db:" +sqle);
return null;
} catch (Exception e) {
System.out.println ("Failed to load Jdbc/odbc driver.");
return null;
}
}
/**
*<br> Method Description: Invoke the stored procedure to view the results of the data
*<br> input parameters: Connection Con database connection
*<br> input parameter: String SQL SQL statement to execute
*<br> return type:
*/
public void execute (Connection con) {
CallableStatement toesup = null;
try {
Con.setautocommit (FALSE);
//Call stored procedure
toesup = Con.preparecall ("{Call P_test (?)}");
//Pass parameters to stored procedure
Toesup.setint (1, 6);
//Execute stored procedures
Toesup.executequery ();
Statement stmt = Con.createstatement ();
ResultSet rs = (ResultSet) stmt.executequery ("SELECT * from TEST");
while (Rs.next ()) {
String ID = rs.getstring (1);
String NAME = rs.getstring (2);
System.out.println (id+ "" +name);
}
Rs.close ();
} catch (SQLException e) {
System.out.println (e);
try{
Toesup.close ();
Con.close ();
}catch (Exception es) {System.out.println (es);}
}
}
/**
*<br> Method Description: Example Demo
*<br> input Parameters:
*<br> return type:
*/
public void Demo () {
try{
jdbcstmconn oc = new Jdbcstmconn ();
Connection conn = Oc.conn ();
Oc.execute (conn);
Conn.close ();
}catch (SQLException se) {
System.out.println (SE);
}catch (Exception e) {
System.out.println (e);
}
}
/**
*<br> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] arg) {
if (arg.length!=3) {
System.out.println ("Use:java jdbcstmconn URL username password");
return;
}
Jdbcstmconn oc = new Jdbcstmconn ();
oc.url = arg[0];
Oc.username=arg[1];
oc.password=arg[2];
Oc.demo ();
}
}