|
|
|
|
package com.util;
|
|
|
|
|
|
|
|
|
|
import java.sql.*;
|
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
|
|
import javax.activation.DataSource;
|
|
|
|
|
import java.sql.*;
|
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.sql.Statement;
|
|
|
|
|
import javax.naming.Context;
|
|
|
|
|
import javax.naming.InitialContext;
|
|
|
|
|
|
|
|
|
|
public class DBO {
|
|
|
|
|
|
|
|
|
|
private Connection conn;
|
|
|
|
|
private Statement stmt;
|
|
|
|
|
private DataSource ds;
|
|
|
|
|
|
|
|
|
|
public DBO()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
public void open()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Class.forName("com.mysql.jdbc.Driver");
|
|
|
|
|
conn=DriverManager.getConnection("jdbc:mysql://localhost/wsdc?useUnicode=true&characterEncoding=gb2312","root","root");
|
|
|
|
|
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
|
|
|
|
|
//conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=xwfb","sa","123");
|
|
|
|
|
stmt=conn.createStatement();
|
|
|
|
|
System.out.println("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
System.err.println("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>: " + ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
<EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ⣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
public void close()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// connMgr.freeConnection("java", conn);
|
|
|
|
|
conn.close();
|
|
|
|
|
System.out.println ("<22>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>");
|
|
|
|
|
}
|
|
|
|
|
catch (SQLException ex)
|
|
|
|
|
{
|
|
|
|
|
System.err.println("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳس<D3B3><D8B3><EFBFBD>: " + ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
ִ<EFBFBD>в<EFBFBD>ѯ
|
|
|
|
|
*/
|
|
|
|
|
public ResultSet executeQuery(String sql) throws SQLException
|
|
|
|
|
{
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rs = stmt.executeQuery(sql);
|
|
|
|
|
System.out.println ("ִ<>в<EFBFBD>ѯ");
|
|
|
|
|
return rs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
ִ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
public int executeUpdate(String sql) throws SQLException
|
|
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ret = stmt.executeUpdate(sql);
|
|
|
|
|
|
|
|
|
|
System.out.println ("ִ<><D6B4><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>");
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
<EFBFBD><EFBFBD>SQL<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뵽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
public void addBatch(String sql) throws SQLException
|
|
|
|
|
{
|
|
|
|
|
stmt.addBatch(sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
ִ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
public int [] executeBatch() throws SQLException
|
|
|
|
|
{
|
|
|
|
|
boolean isAuto=conn.getAutoCommit();
|
|
|
|
|
|
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
|
int [] updateCounts = stmt.executeBatch();
|
|
|
|
|
|
|
|
|
|
// conn.commit();
|
|
|
|
|
|
|
|
|
|
// conn.setAutoCommit(isAuto);
|
|
|
|
|
//conn.setAutoCommit(true);
|
|
|
|
|
return updateCounts;
|
|
|
|
|
}
|
|
|
|
|
public boolean getAutoCommit() throws SQLException
|
|
|
|
|
{
|
|
|
|
|
return conn.getAutoCommit();
|
|
|
|
|
}
|
|
|
|
|
public void setAutoCommit(boolean auto) throws SQLException
|
|
|
|
|
{
|
|
|
|
|
conn.setAutoCommit(auto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void commit() throws SQLException
|
|
|
|
|
{
|
|
|
|
|
conn.commit();
|
|
|
|
|
// this.close();
|
|
|
|
|
}
|
|
|
|
|
public void rollBack() throws SQLException
|
|
|
|
|
{
|
|
|
|
|
conn.rollback();
|
|
|
|
|
// this.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|