数据库连接问题:急!谢谢!

sandwish2000 2003-05-31 09:44:07
为什么我的数据库连接的时候,不管是MySQL还是ACCESS
在Editplus下都可以编译,但是不能运行!
出错如下:

import java.sql.*;


class a
{
public static void main(String[] args)
{
Connection con;
Statement stmt;
try{
//Load JDBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//注意DBQ的写法
String url="jdbc:odbc:driver={Microsoft Access Driver(*.mdb)};DBQ=E:\\test.mdb";
con = DriverManager.getConnection(url,"","");
stmt = con.createStatement();
String query = "select * from info";
ResultSet rs = stmt.executeQuery(query);
while ( rs.next() )
{
String str = rs.getString( "name" );
System.out.print( str );
}
}
catch( Exception e ){
e.printStackTrace();
}
}
}

---------- JDK1.4 RUN ----------
java.lang.NoClassDefFoundError: a
Exception in thread "main"
Output completed (3 sec consumed) - Normal Termination
...全文
58 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tracyfmsi 2003-06-02
  • 打赏
  • 举报
回复
ODBC数据源没有建立好,重新建立
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:test";
flashsj 2003-06-01
  • 打赏
  • 举报
回复
:)
lhzhwin 2003-06-01
  • 打赏
  • 举报
回复
贴错了地方,到数据库开发专栏去吧
sandwish2000 2003-06-01
  • 打赏
  • 举报
回复
没有人知道?
Tianminghui 2003-06-01
  • 打赏
  • 举报
回复
String wordQuery = "select words.wfreq,poss.pos,poss.pfreq from words,poss where words.word=?";


PreparedStatement wordQueryStat=c.prepareStatement(wordQuery);
wordQueryStat.setString(1,word);
r=wordQueryStat.executeQuery();

r.next();

我用这种方法Select(功能更好),我原来也用你那种方法没用(莫名其妙).关于你的错误我没看见啊
gaoxin900 2003-06-01
  • 打赏
  • 举报
回复
难道你有 在 DSN 中设置 响应时间吗?
怎么会有 Output completed (3 sec consumed)。给你个最简单的例子

import java.net.URL;
import java.sql.*;

class TestConnectDB
{
public static void main (String args[])
{
String url = "jdbc:odbc:usersBishe";
String query = "SELECT * FROM users";

try
{
//加载驱动程序
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

//与数据源进行连接
Connection con = DriverManager.getConnection (url);

//检查警告信息
checkForWarning (con.getWarnings ());

//获取一个 DatabaseMetaData 类对象,从而可以获取数据源的信息
DatabaseMetaData dma = con.getMetaData ();

System.out.println("\nConnected to " + dma.getURL());
System.out.println("Driver " + dma.getDriverName());
System.out.println("Version " + dma.getDriverVersion());
System.out.println("");

// 创建 Statement 对象
Statement stmt = con.createStatement ();

// 执行查询语句
ResultSet rs = stmt.executeQuery (query);

// 显示结果集
dispResultSet (rs);

// 关闭结果集
rs.close();

// 关闭 Statement
stmt.close();

// 关闭连接
con.close();
}
catch (SQLException ex)
{
System.out.println ("\n*** SQLException caught ***\n");

while (ex != null)
{
System.out.println ("SQLState: " + ex.getSQLState ());
System.out.println ("Message: " + ex.getMessage ());
System.out.println ("Vendor: " + ex.getErrorCode ());
ex = ex.getNextException ();
System.out.println ("");
}
}
catch (java.lang.Exception ex)
{
// Got some other type of exception. Dump it.
ex.printStackTrace ();
}
}

//-------------------------------------------------------------------
// checkForWarning
// Checks for and displays warnings. Returns true if a warning
// existed
//-------------------------------------------------------------------

private static boolean checkForWarning (SQLWarning warn)
throws SQLException
{
boolean rc = false;

// If a SQLWarning object was given, display the
// warning messages. Note that there could be
// multiple warnings chained together

if (warn != null) {
System.out.println ("\n *** Warning ***\n");
rc = true;
while (warn != null) {
System.out.println ("SQLState: " + warn.getSQLState ());
System.out.println ("Message: " + warn.getMessage ());
System.out.println ("Vendor: " + warn.getErrorCode ());
System.out.println ("");
warn = warn.getNextWarning ();
}
}
return rc;
}

//-------------------------------------------------------------------
// dispResultSet
// Displays all columns and rows in the given result set
//-------------------------------------------------------------------

private static void dispResultSet (ResultSet rs)throws SQLException
{
int i;

// Get the ResultSetMetaData. This will be used for
// the column headings

ResultSetMetaData rsmd = rs.getMetaData ();

// Get the number of columns in the result set

int numCols = rsmd.getColumnCount ();

// Display column headings

for (i=1; i<=numCols; i++)
{
if (i > 1) System.out.print(",");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");

// Display data, fetching until end of the result set

while (rs.next ())
{
// Loop through each column, getting the
// column data and displaying

for (i=1; i<=numCols; i++)
{
if (i > 1) System.out.print(",");
System.out.print(rs.getString(i));
}
System.out.println("");

// Fetch the next result set row

}
}
}
abue 2003-06-01
  • 打赏
  • 举报
回复
典型问题
如果是ODBC看看控制面板里的系统DSN是否配置。
如果是Mysql看classpath里是否有mysql.jar
要么你就再设置一下classpath和path
path %java_home%\bin
set classpath=. ;%java_home%\lib\tools.jar;mysql.jar
试试吧。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧