问题描述
编程工具是myecplise8.6,我在myecplise里已经成功配置好了数据库,但是程序里面异常,请大家帮忙解决以下import java.sql.*; public class SqlTest { private String driverClass = "net.sourceforge.jtds.jdbc.Driver"; private String url ="jdbc:jtds:sqlserver:/localhost:1433/demo"; private String user = "sa"; private String password = ""; private Connection connection; public Connection openConnection() throws SQLException { try { Class.forName(driverClass).newInstance(); System.out.println("Successful"); connection = DriverManager.getConnection(url, user, password); return connection; } catch (Exception e) { throw new SQLException(e.getMessage()); } } public void closeConnection() { try { if (connection != null) connection.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws SQLException{ SqlTest st=new SqlTest(); Connection con=st.openConnection(); Statement stmt = con.createStatement(); ResultSet rs=stmt.executeQuery("select * from STCKSZ"); rs.next(); System.out.println(rs.getInt(1)); } } 异常信息:Exception in thread "main" java.sql.SQLException: net.sourceforge.jtds.jdbc.Driverat test.SqlTest.openConnection(SqlTest.java:25)at test.SqlTest.main(SqlTest.java:39)麻烦大家拉 问题补充:randee 写道
解决方案
你这里连接的是Sql Server数据库吧,好像是驱动错误。1 确定你的相应的jar包已经导入,没有的话就到百度就知道导什么jar包了。2 确定你的driverClass写的绝对正确。3 Class.forName(driverClass).newInstance()不明白是什么意思;把它改成Class.forName(driverClass);你在试试。