我按照以下的办法连接数据库,为什么报错误呢,错误是:(连接的数据库:jdbc:odbc:GoodsSupply驱动程序:JDBC-ODBC Bridge (SQLSRV32.DLL)输入表名info
输入列名(为空时程序结束):name
SQLException:Message:[Microsoft][ODBC SQL Server Driver][SQL Server]对象名 'info' 无效。)
我事先也在数据库中建立库Test和表了,也按照他的设置了,为什么会有错误呢
首先打开控制面板的管理工具,打开数据源(ODBC),在用户DSN里面添加数据源(即你要连接的数据库的名字),在这里假定连接SQL
SERVER
2000的GoodsSupply数据库。名称填写你要连接的数据库的名称(GoodsSupply),然后逐步设置,如果选用了使用SQL-SERVER密码认证的话,就要输入相应的用户名及密码连接到数据库。一路下一步设置完成。
在JAVA里面编写程序进行测试,在这里我的程序是让用户输入任意的表名与与列名,把该列的所有数据输出。源代码如下: import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*; public class ODBCBridge { public static void main(String[] args) {
String url="jdbc:odbc:GoodsSupply";
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 {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载驱动
}catch(ClassNotFoundException e){
System.out.println("Can not load Jdbc-Odbc Bridge Driver");
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con=DriverManager.getConnection(url,"USER","PASSWORD");
//使用SQL-SERVER2000认证
DatabaseMetaData dmd=con.getMetaData(); //DMD为连接的相应情况
System.out.println("连接的数据库:"+dmd.getURL());
System.out.println("驱动程序:"+dmd.getDriverName());
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(SQLException ex) {
System.out.println("SQLException:");
while(ex!=null) {
System.out.println("Message:"+ex.getMessage());
ex=ex.getNextException();
}
}catch(Exception e) {
System.out.println("IOException");
}
}
}
输入列名(为空时程序结束):name
SQLException:Message:[Microsoft][ODBC SQL Server Driver][SQL Server]对象名 'info' 无效。)
我事先也在数据库中建立库Test和表了,也按照他的设置了,为什么会有错误呢
首先打开控制面板的管理工具,打开数据源(ODBC),在用户DSN里面添加数据源(即你要连接的数据库的名字),在这里假定连接SQL
SERVER
2000的GoodsSupply数据库。名称填写你要连接的数据库的名称(GoodsSupply),然后逐步设置,如果选用了使用SQL-SERVER密码认证的话,就要输入相应的用户名及密码连接到数据库。一路下一步设置完成。
在JAVA里面编写程序进行测试,在这里我的程序是让用户输入任意的表名与与列名,把该列的所有数据输出。源代码如下: import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*; public class ODBCBridge { public static void main(String[] args) {
String url="jdbc:odbc:GoodsSupply";
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 {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载驱动
}catch(ClassNotFoundException e){
System.out.println("Can not load Jdbc-Odbc Bridge Driver");
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con=DriverManager.getConnection(url,"USER","PASSWORD");
//使用SQL-SERVER2000认证
DatabaseMetaData dmd=con.getMetaData(); //DMD为连接的相应情况
System.out.println("连接的数据库:"+dmd.getURL());
System.out.println("驱动程序:"+dmd.getDriverName());
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(SQLException ex) {
System.out.println("SQLException:");
while(ex!=null) {
System.out.println("Message:"+ex.getMessage());
ex=ex.getNextException();
}
}catch(Exception e) {
System.out.println("IOException");
}
}
}
解决方案 »
- Java基础问题
- 请教一个关于a--的小问题
- 我有一个interface,含有一个void的process()方法,如何在该方法中返回值而不用return方法,类似于回调函数
- 为什么FTPClient listFiles() 返回的总是0
- JTree的一个问题,速度来解决
- 怎么对STRING型做INPUT VALIDATION
- 请教Swing高手
- 请各位老大帮帮忙,回答一个白痴的白痴问题。
- 为什么我写的服务器程序只能接受长度位5个字符的字符串?
- 帮忙解释一下三个术语:binding/late binding/virtual method invocation
- 问一个极其傻瓜的问题
- 请问如何实现自定义事件?
我应该没有连错表!(那位好心人帮忙,连下数据库,我先谢谢了!*_*)
后来还是用直接连接数据库的方法弄的。
真是郁闷死了。。
如果你要是还不会可以联系我[email protected]