package com.ronghz.excel;
import java.sql.*;
public class Conn{
boolean debug;
Connection con;
Statement stmt;
PreparedStatement pstmt;
ResultSet rs;
ResultSetMetaData resultsMeta;
int rows;
int columns;
String PoolName;
String DBurl = "jdbc:mysql://localhost:3306/";
String DBDriver = "com.mysql.jdbc.Driver";
String ConnStr;
String user = "root";
String password = "0";
public Conn(){
ConnStr = DBurl + "xls";
con = null;
stmt = null;
pstmt = null;
rs = null;
resultsMeta = null;
rows = 0;
columns = 0;
init();
}
public Conn(String dbName){
ConnStr = DBurl + dbName;
con = null;
stmt = null;
pstmt = null;
rs = null;
resultsMeta = null;
rows = 0;
columns = 0;
init();
}
public void init()
{
try{
Class.forName(DBDriver);
con = DriverManager.getConnection(ConnStr,user,password);
}catch(ClassNotFoundException e){
}catch(SQLException e){
}
}
public Connection getCon(){
return con;
}
public Statement getStatement(){
return stmt;
}
public ResultSet executeQuery(String sql) throws SQLException{
if(con == null)
return null;
if(stmt == null)
stmt = con.createStatement(1004, 1007);
try{
rs = stmt.executeQuery(sql);
}
catch(SQLException e){
System.out.println("executeQuery:" + sql + "---" + e.getMessage());
throw e;
}
return rs;
}
public int executeUpdate(String sql) throws SQLException {
if(stmt == null)
stmt = con.createStatement(1004, 1007);
int rowcount = 0;
if(con == null)
return 0;
try
{
rowcount = stmt.executeUpdate(sql);
}
catch(SQLException e)
{
if(debug)
System.out.println("executeUpdate:" + sql + "--" + e.getMessage());
throw e;
}
finally
{
}
return rowcount;
}
public int getColumns() {
int columns = 0;
try
{
resultsMeta = rs.getMetaData();
columns = resultsMeta.getColumnCount();
}
catch(SQLException e) { }
return columns;
}
public int getRows() {
rows = 0;
try
{
rs.last();
rows = rs.getRow();
rs.beforeFirst();
}
catch(SQLException e)
{
System.out.println("getRows error:" + e.getMessage());
}
return rows;
}
protected void finalize() throws Throwable {
super.finalize();
if(rs != null) {
try {
rs.close();
}
catch(SQLException e) {
System.out.println("Conn finalize1: " + e.getMessage());
}
rs = null;
}
if(stmt != null) {
try {
stmt.close();
}
catch(SQLException e) {
System.out.println("Conn finalize2: " + e.getMessage());
}
stmt = null;
}
if(pstmt != null) {
try {
pstmt.close();
}
catch(SQLException e) {
System.out.println("Conn finalize3: " + e.getMessage());
}
pstmt = null;
}
if(con != null) {
try {
if(!con.isClosed())
con.close();
}
catch(SQLException e) {
System.out.println("Conn finalize: " + e.getMessage());
}
con = null;
}
}
public void close() {
try {
finalize();
}
catch(Throwable e) {
System.out.println("conn.close error:" + e.getMessage());
}
}
public void beginTrans() throws SQLException {
try {
con.setAutoCommit(false);
}
catch(SQLException ex) {
ex.printStackTrace();
System.out.print("beginTrans Errors");
throw ex;
}
}
public void commit() throws SQLException {
try {
con.commit();
con.setAutoCommit(true);
} catch(SQLException ex) {
ex.printStackTrace();
System.out.print("Commit Errors");
throw ex;
}
}
public void rollback() {
try {
con.rollback();
con.setAutoCommit(true);
}catch(SQLException ex) {
ex.printStackTrace();
System.out.print("Rollback Errors");
}
}
public boolean getAutoCommit() throws SQLException{
boolean result = false;
try{
result = con.getAutoCommit();
}catch(SQLException ex){
ex.printStackTrace();
System.out.println("getAutoCommit fail" + ex.getMessage());
throw ex;
}
return result;
}
public int getTransactionIsolation()throws SQLException{
int re = 0;
try{
re = con.getTransactionIsolation();
}catch(SQLException e){
e.printStackTrace();
System.out.println("getTransactionIsolation fail" + e.getMessage());
}
return re;
}
public void setFetchSize(int size){
try{
if(pstmt != null){
pstmt.setFetchSize(size);
return;
}
if(stmt == null)
stmt = con.createStatement(1004, 1007);
stmt.setFetchSize(size);
}
catch(SQLException e){
System.out.println("setFetchSize fail:" + e.getMessage());
}
return;
}
public void setMaxRows(int maxRows) throws SQLException{
if(pstmt != null){
pstmt.setMaxRows(maxRows);
return;
}try{
if(stmt == null)
stmt = con.createStatement(1004, 1007);
stmt.setMaxRows(maxRows);
}
catch(Throwable t){
System.out.println("conn.setMaxRows:" + t.getMessage());
}
return;
}
public PreparedStatement prepareStatement(String sql)throws SQLException{
if(pstmt != null){
pstmt.close();
pstmt = null;
}
try{
if (con != null) {
pstmt = con.prepareStatement(sql, 1004, 1007);
}
}catch(Throwable t){
System.out.println("conn.prepareStatement:" + t.getMessage());
System.out.println("SQL:" + sql);
}
return pstmt;
}
public ResultSet executePreQuery()throws SQLException{
if(pstmt != null) {
try{
rs = pstmt.executeQuery();
}catch(SQLException e){
System.out.print("executePreQuery:" + pstmt.toString() + "---" + e.getMessage());
throw e;
}
return rs;
} else {
return null;
}
}
public int executePreUpdate()throws SQLException{
int rowcount = 0;
if(con == null)
return 0;
try {
rowcount = pstmt.executeUpdate();
}catch(SQLException e){
if(debug)
System.out.println("executePreUpdate:" + pstmt.toString() + "---" + e.getMessage());
throw e;
}
return rowcount;
}
}
- 1
- 2
前往页