caoyi
发贴: 32
积分: 0
|
于 2004-04-29 22:28
jameszhang wrote: java.sql.SQLException: No suitable driver 驱动程序与数据库不匹配
当时鹅用ORACLE8的JDBC 访问9I时出过这种错!
我用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
|