当前位置: 技术问答>java相关
求SQL Server 2000 JDBC的用法!(高分)
来源: 互联网 发布时间:2015-06-12
本文导语: 请问微软的SQL Server 2000 JDBC怎么用? 最好能写个例子,谢谢 | 当一个JDBC driver安装后,设置环境变量 import java.sql.*; public class AccessMsSql2000 { private String strServerAddr="";//为数据库所在的机...
请问微软的SQL Server 2000 JDBC怎么用?
最好能写个例子,谢谢
最好能写个例子,谢谢
|
当一个JDBC driver安装后,设置环境变量
import java.sql.*;
public class AccessMsSql2000
{
private String strServerAddr="";//为数据库所在的机器的ip+端口(1433)如:127.0.0.1:1433
private String strDBName="";
private String strUser="";
private String strPasswd="";
public AccessMsSql2000(String tmpServerAddr,String tmpDBName,String tmpUser,String tmpPasswd)
{
strServerAddr=tmpServerAddr;
strDBName=tmpDBName;
strUser=tmpUser;
strPasswd=tmpPasswd;
}
public ResultSet execQuery(String strSql) throws SQLException,ClassNotFoundException
{
System.out.println("begin");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String strUrl="jdbc:microsoft:sqlserver://"+strServerAddr;
Connection con1=DriverManager.getConnection(strUrl,strUser,strPasswd);
System.out.println("connect ok");
con1.setCatalog(strDBName);
Statement stmt=con1.createStatement();
System.out.println("strSql="+strSql);
ResultSet rs=stmt.executeQuery(strSql);
return rs;
}
public void execUpdate(String strSql) throws SQLException,ClassNotFoundException
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String strUrl="jdbc:microsoft:sqlserver://"+strServerAddr;
Connection con1=DriverManager.getConnection(strUrl,strUser,strPasswd);
System.out.println("connect ok");
con1.setCatalog(strDBName);
Statement stmt=con1.createStatement();
System.out.println("strSql="+strSql);
stmt.executeUpdate(strSql);
}
}
import java.sql.*;
public class AccessMsSql2000
{
private String strServerAddr="";//为数据库所在的机器的ip+端口(1433)如:127.0.0.1:1433
private String strDBName="";
private String strUser="";
private String strPasswd="";
public AccessMsSql2000(String tmpServerAddr,String tmpDBName,String tmpUser,String tmpPasswd)
{
strServerAddr=tmpServerAddr;
strDBName=tmpDBName;
strUser=tmpUser;
strPasswd=tmpPasswd;
}
public ResultSet execQuery(String strSql) throws SQLException,ClassNotFoundException
{
System.out.println("begin");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String strUrl="jdbc:microsoft:sqlserver://"+strServerAddr;
Connection con1=DriverManager.getConnection(strUrl,strUser,strPasswd);
System.out.println("connect ok");
con1.setCatalog(strDBName);
Statement stmt=con1.createStatement();
System.out.println("strSql="+strSql);
ResultSet rs=stmt.executeQuery(strSql);
return rs;
}
public void execUpdate(String strSql) throws SQLException,ClassNotFoundException
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String strUrl="jdbc:microsoft:sqlserver://"+strServerAddr;
Connection con1=DriverManager.getConnection(strUrl,strUser,strPasswd);
System.out.println("connect ok");
con1.setCatalog(strDBName);
Statement stmt=con1.createStatement();
System.out.println("strSql="+strSql);
stmt.executeUpdate(strSql);
}
}