[JAVA100 case]059, transaction processing

Source: Internet
Author: User
Tags stmt oracle database

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");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.