package com.hzs.order;import java.io.IOException;
import java.sql.*;
import java.util.Vector;
import java.util.*;public class DBHandle {
// Field
private Vector result = null;
private Connection conn = null;// Construct
public DBHandle() {
}// Connect to database
public void connect() throws DBException, IOException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:system/[email protected]:1521:mydb");
} catch(ClassNotFoundException E) {
throw new DBException("Connnect: " + String.valueOf(E.getMessage()));
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
} public void transaction() throws DBException, IOException {
try {
conn.setAutoCommit(false);
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
}// Executes a SQL statement that returns a single ResultSet.
public Vector select(String strQuery) throws DBException, IOException {
result = new Vector(); try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strQuery);
ResultSetMetaData rsmd = rs.getMetaData();
int FieldNum = rsmd.getColumnCount(); while (rs.next()) {
for (int i = 1; i <= FieldNum; i ++)
result.addElement(rs.getString(i));
} rs.close();
stmt.close();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
return result;
}// Executes an SQL INSERT, UPDATE or DELETE statement.
public int update(String strUpdate) throws DBException, IOException {
int stmtInt=-1;
try {
Statement stmt = conn.createStatement();
stmtInt = stmt.executeUpdate(strUpdate);
stmt.close();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
return stmtInt;
}// commit
public void commit() throws DBException, IOException {
try {
conn.commit();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
}// rollback
public void rollback() throws DBException, IOException {
try {
conn.rollback();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
}// close
public void close() throws DBException, IOException {
try {
conn.close();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
} public static String toChinese(String strValue) {
try {
if (strValue == null)
return null;
else
return new String(strValue.getBytes("8859_1"), "gb2312");
} catch(Exception e) {
return null;
}
} public static float toFloat(String str){
return Float.parseFloat(str);
}
}
import java.sql.*;
import java.util.Vector;
import java.util.*;public class DBHandle {
// Field
private Vector result = null;
private Connection conn = null;// Construct
public DBHandle() {
}// Connect to database
public void connect() throws DBException, IOException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:system/[email protected]:1521:mydb");
} catch(ClassNotFoundException E) {
throw new DBException("Connnect: " + String.valueOf(E.getMessage()));
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
} public void transaction() throws DBException, IOException {
try {
conn.setAutoCommit(false);
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
}// Executes a SQL statement that returns a single ResultSet.
public Vector select(String strQuery) throws DBException, IOException {
result = new Vector(); try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strQuery);
ResultSetMetaData rsmd = rs.getMetaData();
int FieldNum = rsmd.getColumnCount(); while (rs.next()) {
for (int i = 1; i <= FieldNum; i ++)
result.addElement(rs.getString(i));
} rs.close();
stmt.close();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
return result;
}// Executes an SQL INSERT, UPDATE or DELETE statement.
public int update(String strUpdate) throws DBException, IOException {
int stmtInt=-1;
try {
Statement stmt = conn.createStatement();
stmtInt = stmt.executeUpdate(strUpdate);
stmt.close();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
return stmtInt;
}// commit
public void commit() throws DBException, IOException {
try {
conn.commit();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
}// rollback
public void rollback() throws DBException, IOException {
try {
conn.rollback();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
}// close
public void close() throws DBException, IOException {
try {
conn.close();
} catch(SQLException E) {
throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
} catch(Exception E) {
throw new DBException("Exception: " + String.valueOf(E.getMessage()));
}
} public static String toChinese(String strValue) {
try {
if (strValue == null)
return null;
else
return new String(strValue.getBytes("8859_1"), "gb2312");
} catch(Exception e) {
return null;
}
} public static float toFloat(String str){
return Float.parseFloat(str);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货