package common;import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.sql.DataSource;
import java.sql.*;
import java.util.*;public class DBAccess {
private Connection conn=null;
private PreparedStatement ps=null;
private ResultSet rs=null; private DataSource dataSource=null;
private boolean isClosed=false;
private boolean inTransaction;
private String className="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Title";
private String user="sa";
private String password="123";
// public DBAccess(final DataSource ds) {
// super();
// this.dataSource = ds;
// this.conn = null;
// this.isClosed = true;
// this.inTransaction = false;
//
// }
public DBAccess() {
try {
Class.forName(className);
conn= DriverManager.getConnection(url, user,password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public final void open()
throws SQLException {
if (this.isClosed) {
// this.conn = this.dataSource.getConnection();
this.conn=this.getConnection();
}
this.isClosed = false;
}
public final Connection getConnection() {
return this.conn;
}
public void executeUpdate(String sql,Object[]args){//9个操作
//PreparedStatement
try{
ps = this.getConnection().prepareStatement(sql);
if(args!=null){
for(int i=0;i<args.length;i++){
ps.setObject(i+1,args[i]);
}
}
ps.executeUpdate();
}catch(Exception e){}finally{
close();
}
}
public List<Map<String,String>> executeQuery(String sql,Object[]args){
//PreparedStatement 与 ResultSet
List<Map<String,String>> list=null;
try{
ps=this.getConnection().prepareStatement(sql);
if(args!=null){
for(int i=0;i<args.length;i++)
ps.setObject(i+1,args[i]);
}
rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
int count=rsmd.getColumnCount(); list=new ArrayList<Map<String,String>>();
while(rs.next())
{
Map<String,String> map=new HashMap<String,String>();
for(int j=1;j<=count;j++){
map.put(rsmd.getColumnName(j),rs.getString(j));
}
list.add(map);
} }catch(Exception e){
e.printStackTrace();
}finally{
close();
}
return list;
}
public void close(){
//关闭资源
try{
if(rs!=null){rs.close();rs = null;}
}catch(Exception e){}finally{
try{
if(ps!=null){ps.close();ps = null;}
}catch(Exception ee){}finally{
try{
if(this.getConnection()!=null){
this.getConnection().close();
conn = null;
}
}catch(Exception eee){}
}
}
}
public final boolean isClosed() {
return this.isClosed;
}
}
报错;java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]The specified SQL type is not supported by this driver.
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.sql.DataSource;
import java.sql.*;
import java.util.*;public class DBAccess {
private Connection conn=null;
private PreparedStatement ps=null;
private ResultSet rs=null; private DataSource dataSource=null;
private boolean isClosed=false;
private boolean inTransaction;
private String className="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Title";
private String user="sa";
private String password="123";
// public DBAccess(final DataSource ds) {
// super();
// this.dataSource = ds;
// this.conn = null;
// this.isClosed = true;
// this.inTransaction = false;
//
// }
public DBAccess() {
try {
Class.forName(className);
conn= DriverManager.getConnection(url, user,password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public final void open()
throws SQLException {
if (this.isClosed) {
// this.conn = this.dataSource.getConnection();
this.conn=this.getConnection();
}
this.isClosed = false;
}
public final Connection getConnection() {
return this.conn;
}
public void executeUpdate(String sql,Object[]args){//9个操作
//PreparedStatement
try{
ps = this.getConnection().prepareStatement(sql);
if(args!=null){
for(int i=0;i<args.length;i++){
ps.setObject(i+1,args[i]);
}
}
ps.executeUpdate();
}catch(Exception e){}finally{
close();
}
}
public List<Map<String,String>> executeQuery(String sql,Object[]args){
//PreparedStatement 与 ResultSet
List<Map<String,String>> list=null;
try{
ps=this.getConnection().prepareStatement(sql);
if(args!=null){
for(int i=0;i<args.length;i++)
ps.setObject(i+1,args[i]);
}
rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
int count=rsmd.getColumnCount(); list=new ArrayList<Map<String,String>>();
while(rs.next())
{
Map<String,String> map=new HashMap<String,String>();
for(int j=1;j<=count;j++){
map.put(rsmd.getColumnName(j),rs.getString(j));
}
list.add(map);
} }catch(Exception e){
e.printStackTrace();
}finally{
close();
}
return list;
}
public void close(){
//关闭资源
try{
if(rs!=null){rs.close();rs = null;}
}catch(Exception e){}finally{
try{
if(ps!=null){ps.close();ps = null;}
}catch(Exception ee){}finally{
try{
if(this.getConnection()!=null){
this.getConnection().close();
conn = null;
}
}catch(Exception eee){}
}
}
}
public final boolean isClosed() {
return this.isClosed;
}
}
报错;java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]The specified SQL type is not supported by this driver.
你的url用的是2005的 但是你的驱动貌似是2000的