2

Okay, I am getting really irritated because I realize this is something easy but at this point I am not sure what else to try. The program is simply supposed to create a Coin table that stores the names, quantities and values of coins. I had started off with starting the program from the command line but after not getting that to work I changed it up to execute in NetBeans. I keep getting the same error message though. I have searched for solutions and can't find the issue as my driver class appears to be what is required. I am currently using as the driver 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();
     }
    }
    }

And my second class...

    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);
    }  
    }

And the error message in full:

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

I would really appreciate any help. I don't know where I am going wrong. I can connect and create a database by going through services in NetBeans so I don't think it is configured wrong.

Dan Morris
  • 25
  • 1
  • 6
  • Make sure ojdbc6.jar is in your project Properties runtime classpath : http://netbeans.org/kb/docs/java/project-setup.html –  Sep 13 '13 at 21:33

2 Answers2

2

1) Make sure oracle.jar is in your project's runtime classpath :

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

2) It's worth trying oracle.jdbc.Driver.OracleDriver

'Hope that helps

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Okay, that did help some as I am now getting a new batch of error messages. I changed it in the runtime classpath to a mysql.jar as that is the driver I am using. The driver is also listed as com.mysql.jdbc.Driver. I had been tinkering with the oracle before but that runtime class path had not been changed either so I was getting the same message. I will update the post so as not to be confusing. – Dan Morris Mar 02 '13 at 05:56
  • Okay, it is working now. Adding the classpath in the runtime is what did it. I have bookmarked that website for future use. The other error I was getting was just a simple duplication of a column heading from a little too much cutting and pasting maybe. Thanks a lot for your help. – Dan Morris Mar 02 '13 at 06:44
0

What worked for me for the same error, when trying to connect to Postgresql DB with Eclipse on Ubuntu (javaSE 1.6). I went to Eclipse -> Project -> Proprieties -> Java Build Path -> Libraries -> add external jar and added the postgresql-9.3-1100.jdbc4.jar from http://jdbc.postgresql.org/download.html.

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);
        }
    }
Nicoara Talpes
  • 710
  • 1
  • 13
  • 28