女孩溺水死亡照片大全:高手进,急java数据库的连接问题

来源:百度文库 编辑:高校问答 时间:2024/05/09 20:01:10
如何安装jtds驱动,从网站下载下jtds-1.2.zip文件不知如何安装,请教各位大虾安装在的具体目录,连接的数据库还用配置吗,如何用net.sourceforge.jtds.jdbc.Driver连接数据库
我想访问远端SQL SERVER数据库,用哪个驱动比较的好,远端的数据库还用配置吗?我用的是jbuilder9开发

应用程序---JDBC API---驱动程序---数据源

这里首先要安装JDBC的驱动程序,推荐SP2版本的,可从微软网站上下载
http://www.microsoft.com/downloads/details.aspx?FamilyID=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&DisplayLang=en 下载最下面的SETUP.EXE

这个驱动程序要配合SQL SERVER2000 SP3A,相应下载URL为
http://www.microsoft.com/china/sql/downloads/sp3.asp 下载 chs_sql2ksp3.exe

如果用JAVA SDK直接编译运行的话需要设置环境变量,将安装好的JDBC驱动里面的LIB三个文件设置为环境变量:
classpath:
D:\program files\Microsoft SQL Server\jdbc\lib\msbase.jar;
D:\program files\Microsoft SQL Server\jdbc\lib\mssqlserver.jar;
D:\program files\Microsoft SQL Server\jdbc\lib\msutil.jar;

安装即可用微软的驱动程序连接数据库了,相应代码与前面基本相同:

import java.sql.*;
import java.io.*;
public class DBColumn {

public static void main(String[] args) {
Connection con=null;
Statement sm=null;
String command=null;
ResultSet rs=null;
String tableName=null;
String cName=null;
String result=null;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("驱动程序已加载");
//SQL SERVER的登陆方式必须为使用SQL SERVER密码登陆认证方式
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://SERVERNAME:1433","USER","PASSWORD");
con.setCatalog("GoodsSupply");
System.out.println("OK,成功连接到数据库");
}catch(Exception ex) {
ex.printStackTrace();
}
try
{
sm=con.createStatement();
System.out.println("输入表名");
tableName=input.readLine();
while(true) {
System.out.println("输入列名(为空时程序结束):");
cName=input.readLine();
if(cName.equalsIgnoreCase(""))
break;
command="select "+cName+" from "+tableName;
rs=sm.executeQuery(command);
if(!rs.next())
System.out.println("表名或列名输入有误");
else {
System.out.println("查询结果为:");
do
{
result=rs.getString(cName);
//result=new String(result.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
}

a最直接的办法就是直接另存为txt纯文本