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 demonstrates adding data and querying data. </p>
* <p>copyright:copyright (c) 2003</p>
* <p>filename:jdbcconncommit.java</p
* @version 1.0
*/
public class jdbcconncommit{
private static String url= "";
private static String username= "";
private static String password= "";
/**
*<br> method Description: Get data connection
*<br> input parameter:
*<br> return Type: Connection Connection object
/
Public Connect Ion Conn () {
try {
//load JDBC driver
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Create a 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: Executing query SQL statement
*<br> input parameters: Connection Con database connection
*<br> input parameters: String SQL The SQL statement to execute
*<br> return type:
*/
public void query (Connection con, String sql) throws Sqlexception,exception { br> try{
if (con==null) {
throw new Exception ("Database connection can´t use!");
}
if (sql==null) throw new Exception ("Check your parameter:´sql´! Don´t input null! ");
//Declaration statement
Statement stmt = Con.createstatement ();
Execute Query
ResultSet rs = stmt.executequery (sql);
ResultSetMetaData Rmeta = Rs.getmetadata ();
//Get the number of data fields
int numcolumns = Rmeta.getcolumncount ();
while (Rs.next ())
{
for (int i = 0;i< numcolumns;i++)
{
String stemp = rs.getstring (i+1);
System.out.print (stemp+ "");
}
System.out.println ("");
}
Rs.close ();
Stmt.close ();
}catch (Exception e) {
System.out.println ("Query error:sql =" +sql);
System.out.println ("Query error:" +e);
Throw new SQLException ("Query Error");
}
}
/**
*<br> Method Description: Perform inserts, updates, deletes, etc. SQL statements that do not return a result set
*<br> input parameters: Connection Con database connection
*<b r> input parameter: String SQL SQL statement to execute
*<br> return type:
*/
public void execute (Connection con, String sql) throws SQ lexception {
try{
if (con==null) return;
Statement stmt = Con.createstatement ();
int i = stmt.executeupdate (sql);
System.out.println ("Update row:" +i);
Stmt.close ();
}catch (Exception e) {
System.out.println ("Execute error:sql =" +sql);
System.out.println (e);
Throw new SQLException ("Execute Error");
}
}