public class dbcon {// Database-driven object public static final string driver =
"Oracle.jdbc.driver.OracleDriver"; Database connection address (database name) public static final string url = "jdbc:oracle:thin:@
LOCALHOST:1521:ORCL ";
Landing name public static final string user = "Scott";
Login Password public static final string pwd = "123456";
Create a database connection object private connection con = null;
CREATE Database Precompiled objects private preparedstatement ps = null;
Create result set private resultset rs = null;
Create a data source object public static datasource source = null;
Public connection getcon () {try{class.forname (DRIVER);
Con = drivermanager.getconnection (URL, USER, PWD); SYSTEM.OUT.PRINTLN ("Database connection succeeded.")
"); } catch (exception e) {SYSTEM.ERR.PRINTLN ("Database connection failed.")
");
E.printstacktrace ();
} return con;
} public void closeall () {if (rs != null) try {rs.close ();
} catch (sqlexception e) {e.printstacktrace ();
} if (Ps != null) try {ps.close ();
} catch (sqlexception e) {e.printstacktrace ();
} if (Con != null) try {con.close ();
} catch (sqlexception e) {e.printstacktrace (); } public int update (String sql,string... pras) { & nbsp; int resu=0; con=getcon (); try { ps=con.preparestatement (SQL); &nbSp; for (int i=0;i<pras.length;i++) { ps.setstring (I+1,pras[i]); } resu=ps.executeupdate (); } catch (sqlexception e) { } finally{ closeall (); } return resu; } public resultset query (String sql, string... Pras) { con=getcon (); try { ps=con.preparestatement (SQL); if (pras!=null) for (int i=0;i<pras.length;i++) { ps.setstring (I+1, pras[i]); } rs=ps.executequery (); } catch (SQLEXCEPTION&NBsp;e) { // todo Auto-generated catch block e.printstacktrace (); } return rs; } Public static void main (String[] args) throws sqlexception {DBCon
dbcon = new dbcon ();
Dbcon.getcon ();
Resultset resultset = dbcon.query ("Select * from emp", null);
while (Resultset.next ()) {System.out.printf ("%-10d", Resultset.getint (1));
//output 10 columns, left-aligned (-number denotes left-aligned)//System.out.print (Resultset.getint (1) + "\t\t"); System.out.printf ("%-10s", resultset.getstring (2)); //SysteM.out.print (resultset.getstring (2) + "\t\t"); System.out.printf ("%-20s", Resultset.getstring (3)); //System.out.print (resultset.getstring (3) +
"\t\t\t\t"); System.out.printf ("%-10d", Resultset.getint (4)); //System.out.print (Resultset.getint (4) + "\t\t"
);
System.out.print (Resultset.getdate (5) + "\t\t");
System.out.print (resultset.getdouble (6) + "\t\t");
System.out.print (resultset.getdouble (7) + "\t\t");
System.out.println (Resultset.getint (8));
} dbcon.closeall (); }
}
Jar Pack Download Http://yunpan.cn/QC5IeekKJvixN extract code 243b
Java Operations Oracle Implementation Transaction rollback
Dbcon dbcon = new dbcon ();
Connection connection = dbcon.getcon ();
string sql = "";
statement statement = null; try {Connection.setautocommit (FALSE);/set things are not automatically submitted sql = "update emp set sal =
sal - 100 where ename = ' SMITH ';
Statement = connection.createstatement ();
Statement.executeupdate (SQL);
int i = 7/0; //throws an exception. sql = "update emp set sal = sal - 100 where ename
= ' JAMES ';
Statement.executeupdate (SQL);
Connection.commit (); } catch (exception e) {/Note If this is SqlException, the exception thrown above 7/0 will not be captured here, and the following rollback will naturally not be executed. Trap try {connection.rollback ()//Transaction rollback} catch (sqlexception e1) {E1.printstacktrac
E ();
} e.printstacktrace (); }finally{DBCON.CLoseall ();
if (statement != null) {try {statement.close ();
} catch (sqlexception e) {e.printstacktrace (); }
}
}
By default, things are submitted automatically, and when an exception is thrown in the previous example, the preceding SQL statement succeeds and the latter is not executed. After the things are managed, after the exception is thrown in the example above, things are rolled back in the catch block below. Ensure that two SQL statements are not executed.
This article is from the "Avatar" blog, please be sure to keep this source http://shamrock.blog.51cto.com/2079212/1532489