当前位置: 技术问答>java相关
jdbc--sqlserver报错
来源: 互联网 发布时间:2015-03-17
本文导语: 想连接SQL SERVER,安装的是jdbc driver: com.inet.tds.TdsDriver 将驱动程序 freetds_jdbc.jar 拷贝到d:jbuilder5jdk1.3LIB下面 autoexec.bat------- SET JAVA_HOME=d:jbuilder5jdk1.3 set classpath=./;d:jbuilder5jdk1.3bintools.jar;d:jbuilder5jdk1.3libdt.jar;d:jbuilder...
想连接SQL SERVER,安装的是jdbc driver: com.inet.tds.TdsDriver
将驱动程序 freetds_jdbc.jar 拷贝到d:jbuilder5jdk1.3LIB下面
autoexec.bat-------
SET JAVA_HOME=d:jbuilder5jdk1.3
set classpath=./;d:jbuilder5jdk1.3bintools.jar;d:jbuilder5jdk1.3libdt.jar;d:jbuilder5jdk1.3lib
path=%path%;d:jbuilder5jdk1.3bin;d:jbuilder5jdk1.3;d:tomcatbin
代码:--------------
import java.sql.*;
public class DbSql
{
public static void main(String args[])
{
try
{
Class.forName("com.inet.tds.TdsDriver");
} catch (Exception e) {}
try
{
String url = "jdbc:inetdae:\202.121.67.141:1433";
String login = "administrator";
String password = "tzy";
Connection connection= DriverManager.getConnection(url,login,password);
DatabaseMetaData conMD = connection.getMetaData();
connection.setCatalog("Graduate");
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT size FROM sysfiles");
while (rs.next())
{
System.out.println(String.valueOf(rs.getInt("size")));
}
System.out.println();
st.close();
connection.close();
}
catch(java.sql.SQLException e){System.out.println(e.toString());}
}
}
编译不报错
执行报错
E:StudyjavaTest>java DbSql
java.sql.SQLException: No suitable driver
将驱动程序 freetds_jdbc.jar 拷贝到d:jbuilder5jdk1.3LIB下面
autoexec.bat-------
SET JAVA_HOME=d:jbuilder5jdk1.3
set classpath=./;d:jbuilder5jdk1.3bintools.jar;d:jbuilder5jdk1.3libdt.jar;d:jbuilder5jdk1.3lib
path=%path%;d:jbuilder5jdk1.3bin;d:jbuilder5jdk1.3;d:tomcatbin
代码:--------------
import java.sql.*;
public class DbSql
{
public static void main(String args[])
{
try
{
Class.forName("com.inet.tds.TdsDriver");
} catch (Exception e) {}
try
{
String url = "jdbc:inetdae:\202.121.67.141:1433";
String login = "administrator";
String password = "tzy";
Connection connection= DriverManager.getConnection(url,login,password);
DatabaseMetaData conMD = connection.getMetaData();
connection.setCatalog("Graduate");
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT size FROM sysfiles");
while (rs.next())
{
System.out.println(String.valueOf(rs.getInt("size")));
}
System.out.println();
st.close();
connection.close();
}
catch(java.sql.SQLException e){System.out.println(e.toString());}
}
}
编译不报错
执行报错
E:StudyjavaTest>java DbSql
java.sql.SQLException: No suitable driver
|
你的代码似乎是用:i-net jdbc driver, 而jar文件是freetds.org的产品。二者混用了。
tds只是一个协议标准,很多产品都是依据tds.
如果用:freetds_jdbc.jar ,
正确的代码:
...
Class.forName("com.internetcds.jdbc.tds.Driver");
...
String url = "jdbc:freetds:sqlserver://server:port/database";
注释掉如下两行:
//DatabaseMetaData conMD = connection.getMetaData();
//connection.setCatalog("Graduate");
tds只是一个协议标准,很多产品都是依据tds.
如果用:freetds_jdbc.jar ,
正确的代码:
...
Class.forName("com.internetcds.jdbc.tds.Driver");
...
String url = "jdbc:freetds:sqlserver://server:port/database";
注释掉如下两行:
//DatabaseMetaData conMD = connection.getMetaData();
//connection.setCatalog("Graduate");