Topic: 【求助】关于jdbc驱动设置的问题。 |
Print this page |
1.【求助】关于jdbc驱动设置的问题。 | Copy to clipboard |
Posted by: caoyi Posted on: 2004-04-29 21:30 我现在拥有jdbc的mysql-connector和servlet-api的驱动。当我把他设置在jcreator中作为archive时候,连驱动的程序可以正常的编译成功和运行。但是我在命令提示符中运行已经编译的程序却说: "java.sql.SQLException: No suitable driver at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source)" 我的classpath和path都设置了jdk目录bin目录上,且后面加了;.;编译其他没有驱动的程序以及运行他们都正常。不知道除了classpath和path环境变量设置外还要针对驱动设置些什么参数吗? 百忙之中抽时间看我这个帖子万分感谢。也非常感激能够给我回复。谢谢。 |
2.Re:【求助】关于jdbc驱动设置的问题。 [Re: caoyi] | Copy to clipboard |
Posted by: jameszhang Posted on: 2004-04-29 21:51 java.sql.SQLException: No suitable driver 驱动程序与数据库不匹配 当时鹅用ORACLE8的JDBC 访问9I时出过这种错! |
3.Re:【求助】关于jdbc驱动设置的问题。 [Re: jameszhang] | Copy to clipboard |
Posted by: caoyi Posted on: 2004-04-29 22:28 jameszhang wrote: 我用jc编译和运行都正常。该程序就是corejava2中的例子。我在jc中是把驱动设置为archive。驱动放在j2sdk1.4.2_04\jre\lib\ext下。 import java.io.*; import java.util.*; import java.sql.*; class ExecSQL { public static void main(String args[]) { try { Reader reader; if(args.length==0) reader = new InputStreamReader(System.in); else reader = new FileReader(args[0]); Connection conn = getConnection(); Statement stat = conn.createStatement(); BufferedReader in = new BufferedReader(reader); boolean done = false; while(!done) { if(args.length == 0) System.out.println( "Enter command or a blank line to exit:"); String line = in.readLine(); if(line == null||line.length() == 0) done = true; else { try { boolean hasResultSet = stat.execute(line); if(hasResultSet) showResultSet(stat); } catch(SQLException ex) { while(ex != null) { ex.printStackTrace(); ex = ex.getNextException(); } } } } in.close(); stat.close(); conn.close(); } catch(Exception ex) { ex.printStackTrace(); } } public static Connection getConnection() throws SQLException, IOException { Properties props = new Properties(); FileInputStream in = new FileInputStream("database.properties"); props.load(in); in.close(); String drivers = props.getProperty("jdbc.drivers"); if(drivers != null) System.setProperty("jdbc.drivers", drivers); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); return DriverManager.getConnection(url, username, password); } public static void showResultSet(Statement stat) throws SQLException { ResultSet result = stat.getResultSet(); ResultSetMetaData metaData = result.getMetaData(); int columnCount = metaData.getColumnCount(); for(int i = 1;i <= columnCount; i++) { if(i > 1) System.out.print(", "); System.out.print(metaData.getColumnLabel(i)); } System.out.println(); while(result.next()) { for(int i = 1; i <= columnCount; i++) { if(i > 1) System.out.print(", "); System.out.print(result.getString(i)); } System.out.println(); } result.close(); } } 文件database.properties jdbc.drivers=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=blue jdbc.password=blue |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |