com.mysql.jdbc.Driver在尝试创建数据库时抛出ClassNotFoundException

时间:2013-03-02 05:06:03

标签: java database jdbc classnotfound

好的,我真的很烦,因为我觉得这很容易,但此时我不知道还有什么可以尝试的。该程序只是创建一个硬币表,存储硬币的名称,数量和价值。我从命令行启动程序开始,但在没有让它工作之后,我将其更改为在NetBeans中执行。我不断收到相同的错误消息。我搜索了解决方案,找不到问题,因为我的驱动程序类似乎是必需的。我目前正在使用驱动程序com.mysql.jdbc.Driver。

    public class CoinDataBase {

     static String file = "C:\\Users\\Dan\\Desktop\\database.properties.txt";

     public static void main(String[] args) throws SQLException, IOException,
        ClassNotFoundException {


     SimpleDataSource.init(file);

     Connection conn = SimpleDataSource.getConnection();
     try
     {
        Statement stat = conn.createStatement();

        stat.execute("CREATE TABLE Coin (Name VARCHAR(12),Value "
                + "DECIMAL(5,2),QTY DECIMAL(5,0),Value DECIMAL(5,2)"
                + ",Total DECIMAL(5,2))");
        stat.execute("INSERT INTO Coin VALUES('Penny',.01,5,.05)");
        stat.execute("INSERT INTO Coin VALUES('Nickel',.05,2,.10)");
        stat.execute("INSERT INTO Coin VALUES('Dime',.10,3,.30)");
        stat.execute("INSERT INTO Coin VALUES('Quarter',.25,2,.50)");
        stat.execute("INSERT INTO Coin VALUES('Half Dollar',.50,3,1.50)");
        stat.execute("INSERT INTO Coin VALUES('Dollar',1.00,2,2.00)"); 

        ResultSet result = stat.executeQuery("SELECT * FROM Coin");
        result.next();
        System.out.println(result.getString("Name")); 
     }
     finally
     {
        conn.close();
     }
    }
    }

我的第二堂课......

    class SimpleDataSource {

    private static String url, username, password;

    static void init(String fileName) throws IOException, ClassNotFoundException
    {
      Properties props = new Properties();
      FileInputStream in = new FileInputStream(fileName);
      props.load(in);

      String driver = props.getProperty("jdbc.driver");
      url = props.getProperty("jdbc.url");
      username = props.getProperty("jdbc.username");
      if (username == null) username = "";
      password = props.getProperty("jdbc,password");
      if (password == null) password = "";
      if (driver != null)
        Class.forName(driver);  
    }

     static Connection getConnection() throws SQLException{
     return DriverManager.getConnection(url, username, password);
    }  
    }

完整的错误消息:

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at coindatabase.SimpleDataSource.init(SimpleDataSource.java:28)
    at coindatabase.CoinDataBase.main(CoinDataBase.java:20)
Java Result: 1

我真的很感激任何帮助。我不知道我哪里出错了。我可以通过浏览NetBeans中的服务来连接和创建数据库,因此我认为它配置错误。

2 个答案:

答案 0 :(得分:2)

1)确保oracle.jar位于项目的运行时类路径中:

http://netbeans.org/kb/docs/java/project-setup.html

2)值得尝试oracle.jdbc.Driver.OracleDriver

'希望有所帮助

答案 1 :(得分:0)

在Ubuntu(javaSE 1.6)上尝试使用Eclipse连接到Postgresql DB时,对于同样的错误有什么用处。我去了Eclipse - >项目 - >礼物 - > Java构建路径 - >图书馆 - >添加外部jar并从http://jdbc.postgresql.org/download.html添加postgresql-9.3-1100.jdbc4.jar。

Connection con = null;
    Statement st = null;
    ResultSet rs = null;


    String url = "jdbc:postgresql://localhost:5432/unnamed_db";
    String user = "postgres";
    String password = "*";

    try {
        con = DriverManager.getConnection(url, user, password);
        st = con.createStatement();
        rs = st.executeQuery("select * from table;");

        if (rs.next()) {
            System.out.println(rs.getString(1));
        }

    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(Version.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);

    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.WARNING, ex.getMessage(), ex);
        }
    }