经典的错误,经典的解答 装载驱动程序出错了,根据错误信息,应该是驱动程序参数没写完整在java中,连接数据库时,应该采用你采用的数据库提供商所提供的驱动程序 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 sqlstr = "select * from employee where employeeName='"+ms+"'"; string url = "jdbc:odbc:shop";修改为String url = "jdbc:odbc:Driver = {Microsoft Access Driver (*.mdb)}; DBQ=C:/CDShop.mdb";运行后是同样的错误 按zjpangxie(买套房子咋就那么麻烦) 大哥的方法显示错误:无效的描述器索引 将你的sql语句直接在access下运行看看。 如:zjpangxie(买套房子咋就那么麻烦)所言,你的sqlstr的写法不对: 应该写成:"select * from employee where employeeName='"+ms+"'"; import java.sql.*;public class Test{ public static void main(String[] args){ String ms = "sun"; String sqlstr = "select * from employee where employeeName = 'ms'"; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:shop"; Connection conn = DriverManager.getConnection(url,"",""); Statement stmtNew = conn.createStatement(); ResultSet s= stmtNew.executeQuery(sqlstr); s.next(); System.out.println(s.getString(2)); }catch(SQLException e1){ System.out.println(e1.getMessage()); }catch(Exception e2){ System.out.println("出错了"); } }}========================================在我的机器上可以运行,不过你的保证Employee表中有记录 sqlstr的写法不对, 应该写成:sqlstr="select * from employee where employeeName='"+ms+"'"; sqlstr="select * from employee where employeeName='"+ms+"'";ms是变量应该"+ms+"这样引用 String ms = "sun";sqlstr = "select * from employee where employeeName = ms";----->干嘛不直接这样呢String sqlstr = "select * from employee where employeeName = sun";或者你用PreparedStatement吧。灵活性更大一些 string url = "jdbc:odbc:shop";'string'这样定义可以吗? resultset s= stmtNew.executeQuery(sqlstr); ResultSet s = conn.createStatement();里边应该有参数!conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); conn.createStatement();里边应该有参数!conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);而且,SQL应该是sqlstr = "select * from employee where employeeName='"+ms+"'"; conn.createStatement();里边应该有参数!conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);而且,SQL应该是sqlstr = "select * from employee where employeeName='"+ms+"'"; sqlstr = "select * from employee where employeeName = ms";写错了,应该为sqlstr = "select * from employee where employeeName ='" + ms + "'"; 变量连接有问题,要加 +变量+sqlstr = "select * from employee where employeeName = ms";写错了,应该为sqlstr = "select * from employee where employeeName ='" + ms + "'"; “我个人喜欢用PreparedStatement ”String ms = "sun";sqlstr = "select * from employee where employeeName = ?";try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); string url = "jdbc:odbc:shop"; Connection conn = DriverManager.getConnection(url, "", ""); PreparedStatement stmtNew = conn.prepareStatement(sqlstr); stmtNew.setString(1, ms); ResultSet rs = stmtNew.executeQuery(); /* * 做你想做的操作 */ if(rs!=null) //最重要别忘记关闭哦,不然执行多了就会有错误。 rs.close(); if(stmtNew!=null) stmtNew.close();} catch (SQLException e1) { System.out.println(e1.getMessage());} catch (Exception e2) { System.out.println("出错了");} String sqlstr = "select * from employee where employeeName =" + ms; 设置菜单栏和菜单以及菜单项的外观 关于JTable的几个问题 入门请教各位 急求,读取数据库的中文问题????? 越早做项目越好!如何找项目? 关于解析HTML的元素问题,请各位高手指点一下,分不够再加也没问题 在java中如何声明全局常量。 JDK & JBuilder的疑问 关于创建一临时文件后删除的问题(deteleOnExit()何时起作用) 关于try catch的一个小问题,请大家指点一下 哥哥们帮忙啊!!windows2000下具体怎样配置mysql 4.0.13+tomcat-4.1.24+j2sdk-1_4_2??急用!!~~~谢谢 请问如何隐藏jdbtable的一列
修改为
String url = "jdbc:odbc:Driver = {Microsoft Access Driver (*.mdb)}; DBQ=C:/CDShop.mdb";
运行后是同样的错误
显示错误:无效的描述器索引
public static void main(String[] args){
String ms = "sun";
String sqlstr = "select * from employee where employeeName = 'ms'";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:shop";
Connection conn = DriverManager.getConnection(url,"","");
Statement stmtNew = conn.createStatement();
ResultSet s= stmtNew.executeQuery(sqlstr);
s.next();
System.out.println(s.getString(2));
}catch(SQLException e1){
System.out.println(e1.getMessage());
}catch(Exception e2){
System.out.println("出错了");
} }
}========================================在我的机器上可以运行,不过你的保证Employee表中有记录
应该写成:
sqlstr="select * from employee where employeeName='"+ms+"'";
ms是变量应该"+ms+"这样引用
sqlstr = "select * from employee where employeeName = ms";
----->干嘛不直接这样呢
String sqlstr = "select * from employee where employeeName = sun";或者你用PreparedStatement吧。灵活性更大一些
'string'这样定义可以吗?
里边应该有参数!
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
里边应该有参数!
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);而且,SQL应该是
sqlstr = "select * from employee where employeeName='"+ms+"'";
里边应该有参数!
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);而且,SQL应该是
sqlstr = "select * from employee where employeeName='"+ms+"'";
写错了,应该为
sqlstr = "select * from employee where employeeName ='" + ms + "'";
写错了,应该为
sqlstr = "select * from employee where employeeName ='" + ms + "'";
String ms = "sun";
sqlstr = "select * from employee where employeeName = ?";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
string url = "jdbc:odbc:shop";
Connection conn = DriverManager.getConnection(url, "", "");
PreparedStatement stmtNew = conn.prepareStatement(sqlstr);
stmtNew.setString(1, ms);
ResultSet rs = stmtNew.executeQuery();
/*
* 做你想做的操作
*/
if(rs!=null) //最重要别忘记关闭哦,不然执行多了就会有错误。
rs.close();
if(stmtNew!=null)
stmtNew.close();
} catch (SQLException e1) {
System.out.println(e1.getMessage());
} catch (Exception e2) {
System.out.println("出错了");
}