mssql.javapackage com.rmyy;
import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.naming.*;public class MSsql{
private Connection conn=null;
private Statement stmt=null;
private PreparedStatement prepstmt=null;
/**
*
*
*
**/
public MSsql(){
try{
getDataSource();
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=rmyy";
//String user="sa";
//String password="sa";
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
}catch(Exception e){
System.err.println("MSsql init error:"+e);
}
}
private void getDataSource() {
try {
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Boom - No Context"); DataSource ds =
(DataSource) ctx.lookup("java:comp/env/jdbc/userDB");
if (ds != null)
conn = ds.getConnection(); } catch (Exception e) {
System.err.println("getDataSource() error: " + e);
}
}
/**
*
*
*
**/
public MSsql(String sql){
try{
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=rmyy";
//String user="sa";
//String password="sa";
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//conn=DriverManager.getConnection(url,user,password);
getDataSource();
prepareStatement(sql);
}catch(Exception e){
System.err.println("MSsql init error:"+e);
}
}
public Connection getConnection(){
return conn;
}
public void prepareStatement(String sql) throws SQLException{
prepstmt=conn.prepareStatement(sql);
}
public void setString(int index, String value) throws SQLException {
prepstmt.setString(index, value);
}
public void setInt(int index, int value) throws SQLException {
prepstmt.setInt(index, value);
}
public void setBoolean(int index, boolean value) throws SQLException {
prepstmt.setBoolean(index, value);
}
public void setDate(int index, Date value) throws SQLException {
prepstmt.setDate(index, value);
}
public void setLong(int index, long value) throws SQLException {
prepstmt.setLong(index, value);
}
public void setFloat(int index, float value) throws SQLException {
prepstmt.setFloat(index, value);
}
public void setBinaryStream(int index, InputStream in, int length) throws SQLException {
prepstmt.setBinaryStream(index, in, length);
}
public void clearParameters() throws SQLException {
prepstmt.clearParameters();
}
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
public Statement getStatement() {
return stmt;
}
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null){
return stmt.executeQuery(sql);
} else
return null;
} /**
*
*
*
**/
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
} else
return null;
} /**
*
*
*
**/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
/**
*
*
*
**/
public int executeUpdate() throws SQLException {
int i=0;
if (prepstmt != null)
i=prepstmt.executeUpdate();
return i;
} /**
*
*
*
**/
public void close() {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
} catch (Exception e) {
System.err.println("MSsql close error: " + e);
}
}
}
import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.naming.*;public class MSsql{
private Connection conn=null;
private Statement stmt=null;
private PreparedStatement prepstmt=null;
/**
*
*
*
**/
public MSsql(){
try{
getDataSource();
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=rmyy";
//String user="sa";
//String password="sa";
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
}catch(Exception e){
System.err.println("MSsql init error:"+e);
}
}
private void getDataSource() {
try {
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Boom - No Context"); DataSource ds =
(DataSource) ctx.lookup("java:comp/env/jdbc/userDB");
if (ds != null)
conn = ds.getConnection(); } catch (Exception e) {
System.err.println("getDataSource() error: " + e);
}
}
/**
*
*
*
**/
public MSsql(String sql){
try{
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=rmyy";
//String user="sa";
//String password="sa";
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//conn=DriverManager.getConnection(url,user,password);
getDataSource();
prepareStatement(sql);
}catch(Exception e){
System.err.println("MSsql init error:"+e);
}
}
public Connection getConnection(){
return conn;
}
public void prepareStatement(String sql) throws SQLException{
prepstmt=conn.prepareStatement(sql);
}
public void setString(int index, String value) throws SQLException {
prepstmt.setString(index, value);
}
public void setInt(int index, int value) throws SQLException {
prepstmt.setInt(index, value);
}
public void setBoolean(int index, boolean value) throws SQLException {
prepstmt.setBoolean(index, value);
}
public void setDate(int index, Date value) throws SQLException {
prepstmt.setDate(index, value);
}
public void setLong(int index, long value) throws SQLException {
prepstmt.setLong(index, value);
}
public void setFloat(int index, float value) throws SQLException {
prepstmt.setFloat(index, value);
}
public void setBinaryStream(int index, InputStream in, int length) throws SQLException {
prepstmt.setBinaryStream(index, in, length);
}
public void clearParameters() throws SQLException {
prepstmt.clearParameters();
}
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
public Statement getStatement() {
return stmt;
}
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null){
return stmt.executeQuery(sql);
} else
return null;
} /**
*
*
*
**/
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
} else
return null;
} /**
*
*
*
**/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
/**
*
*
*
**/
public int executeUpdate() throws SQLException {
int i=0;
if (prepstmt != null)
i=prepstmt.executeUpdate();
return i;
} /**
*
*
*
**/
public void close() {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
} catch (Exception e) {
System.err.println("MSsql close error: " + e);
}
}
}
谢谢~~