import java.sql.*; import java.io.*; import java.util.*;
class TestDB { public static void main (String args[]) { try { Connection conn = getConnection(); Statement stat = conn.createStatement();
stat.execute("CREATE TABLE Greetings (Name CHAR(20))"); stat.execute( "INSERT INTO Greetings VALUES ('Hello, DataBase!')");
ResultSet result = stat.executeQuery("SELECT * FROM Greetings"); while (result.next()) System.out.println(result.getString(1)); result.close();
stat.execute("DROP TABLE Greetings"); stat.close(); conn.close(); } catch (SQLException ex) { while (ex != null) { ex.printStackTrace(); ex = ex.getNextException(); } } catch (IOException ex) { ex.printStackTrace(); } }
/* Gets a connection from the properties specified in the file database.properties */ 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"); // return DriverManager.getConnection(url, props); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url, username, password); } } 一道java和数据库的连接代码! 编译可以完成,但运行时抛出 SQLException异常,no suiable driver 为什么????????????????????? 请高手指点,