当前位置: 技术问答>java相关
?那位大虾有jdbc2.0连接sqlserver的例子,不要使用jdbc-odbc桥,要用sqlserver2000jdbc驱动!大哥帮我一下吧!
来源: 互联网 发布时间:2015-03-03
本文导语: | 请看: import java.sql.*; import javax.servlet.*; public class connSql { private String strDBDriver=null; private String strConn=null; private String strDataBase=null; private String strUser=null; private String strPassWord=null; private String strHint=...
|
请看:
import java.sql.*;
import javax.servlet.*;
public class connSql
{
private String strDBDriver=null;
private String strConn=null;
private String strDataBase=null;
private String strUser=null;
private String strPassWord=null;
private String strHint=null;
private int nConnCount=0;
private Connection conn=null;
private ResultSet rs=null;
private Statement stmt=null;
public connSql()
{
//Microsoft Sql Server 2000 JDBC
strDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
strConn="jdbc:microsoft:sqlserver://sq:1433";
//opta 2000 JDBC
//strDBDriver="com.inet.tds.TdsDriver";
//strConn="jdbc:inetdae:sq:1433?sql7=true";
//common variable
strHint="Initializing...";
nConnCount=0;
strDataBase="club";
strUser="sa";
strPassWord="";
}
public String getStrHint()
{
return strHint;
}
public int getNConnCount()
{
return nConnCount;
}
public String getDBDriver()
{
return strDBDriver;
}
public String getConn()
{
return strConn;
}
public boolean initDBDriver()
{
try
{
Class.forName(strDBDriver);
strHint="Initialization successfully";
return true;
}
catch(java.lang.ClassNotFoundException err)
{
strHint="Initialization :"+err.getMessage();
return false;
}
}
public ResultSet executeQuery(String strSql)
{
rs=null;
try
{
conn=DriverManager.getConnection(strConn,strUser,strPassWord);
conn.setCatalog(strDataBase);
stmt=conn.createStatement();
rs=stmt.executeQuery(strSql);
nConnCount++;
}
catch(SQLException err)
{
strHint="executeQuery: "+err.getMessage();
}
return rs;
}
public boolean executeUpdate(String strSql)
{
try
{
conn=DriverManager.getConnection(strConn,strUser,strPassWord);
conn.setCatalog(strDataBase);
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
nConnCount++;
return true;
}
catch(SQLException err)
{
strHint="executeUpdate: "+err.getMessage();
return false;
}
}
public boolean closeConn()
{
try
{
if(rs!=null)
{
rs.close();
}
if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
}
return true;
}
catch(SQLException err)
{
strHint="closeConnection: "+err.getMessage();
return false;
}
}
public static String parseChinese(String in)
{
String s=null;
byte temp[];
if(in==null)
{
System.out.println("Warn:null");
return new String("");
}
try
{
temp=in.getBytes("iso-8859-1");
s=new String(temp);
}
catch(Exception err)
{
System.out.println(err.toString());
}
return s;
}
public static void main(String[] args)
{
connSql clsTemp=new connSql();
System.out.println(clsTemp.getStrHint());
ResultSet rsTemp=null;
String strTemp=null;
try
{
clsTemp.initDBDriver();
rsTemp=clsTemp.executeQuery("select * from dept");
while(rsTemp.next())
{
//strTemp=parseChinese(rsTemp.getString("dname"));
strTemp=(rsTemp.getString("dname"));
System.out.println(strTemp);
}
System.out.println("THE COUNT OF CONNECTION IS:"+clsTemp.getNConnCount());
clsTemp.closeConn();
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}
}
import java.sql.*;
import javax.servlet.*;
public class connSql
{
private String strDBDriver=null;
private String strConn=null;
private String strDataBase=null;
private String strUser=null;
private String strPassWord=null;
private String strHint=null;
private int nConnCount=0;
private Connection conn=null;
private ResultSet rs=null;
private Statement stmt=null;
public connSql()
{
//Microsoft Sql Server 2000 JDBC
strDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
strConn="jdbc:microsoft:sqlserver://sq:1433";
//opta 2000 JDBC
//strDBDriver="com.inet.tds.TdsDriver";
//strConn="jdbc:inetdae:sq:1433?sql7=true";
//common variable
strHint="Initializing...";
nConnCount=0;
strDataBase="club";
strUser="sa";
strPassWord="";
}
public String getStrHint()
{
return strHint;
}
public int getNConnCount()
{
return nConnCount;
}
public String getDBDriver()
{
return strDBDriver;
}
public String getConn()
{
return strConn;
}
public boolean initDBDriver()
{
try
{
Class.forName(strDBDriver);
strHint="Initialization successfully";
return true;
}
catch(java.lang.ClassNotFoundException err)
{
strHint="Initialization :"+err.getMessage();
return false;
}
}
public ResultSet executeQuery(String strSql)
{
rs=null;
try
{
conn=DriverManager.getConnection(strConn,strUser,strPassWord);
conn.setCatalog(strDataBase);
stmt=conn.createStatement();
rs=stmt.executeQuery(strSql);
nConnCount++;
}
catch(SQLException err)
{
strHint="executeQuery: "+err.getMessage();
}
return rs;
}
public boolean executeUpdate(String strSql)
{
try
{
conn=DriverManager.getConnection(strConn,strUser,strPassWord);
conn.setCatalog(strDataBase);
stmt=conn.createStatement();
stmt.executeUpdate(strSql);
nConnCount++;
return true;
}
catch(SQLException err)
{
strHint="executeUpdate: "+err.getMessage();
return false;
}
}
public boolean closeConn()
{
try
{
if(rs!=null)
{
rs.close();
}
if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
}
return true;
}
catch(SQLException err)
{
strHint="closeConnection: "+err.getMessage();
return false;
}
}
public static String parseChinese(String in)
{
String s=null;
byte temp[];
if(in==null)
{
System.out.println("Warn:null");
return new String("");
}
try
{
temp=in.getBytes("iso-8859-1");
s=new String(temp);
}
catch(Exception err)
{
System.out.println(err.toString());
}
return s;
}
public static void main(String[] args)
{
connSql clsTemp=new connSql();
System.out.println(clsTemp.getStrHint());
ResultSet rsTemp=null;
String strTemp=null;
try
{
clsTemp.initDBDriver();
rsTemp=clsTemp.executeQuery("select * from dept");
while(rsTemp.next())
{
//strTemp=parseChinese(rsTemp.getString("dname"));
strTemp=(rsTemp.getString("dname"));
System.out.println(strTemp);
}
System.out.println("THE COUNT OF CONNECTION IS:"+clsTemp.getNConnCount());
clsTemp.closeConn();
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}
}