Results 1 to 10 of 10
  1. #1
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default Java with Data Base

    Can someone help me with this code:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package _darbas;
    import java.sql.*;
    
    /**
     *
     * @author Nerijus
     */
    public class Main {
    
         /********************************************************/
        public static void loadDriver()
        {
           try {
            Class.forName("org.postgresql.Driver");
            // Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    
           }
           catch (ClassNotFoundException cnfe) {
             System.out.println("Couldn't find driver class!");
             cnfe.printStackTrace();
             System.exit(1);
           }
        }
        /********************************************************/
        public static Connection getConnection() {
           Connection postGresConn = null;
           try {
             postGresConn = DriverManager.getConnection("jdbc:postgresql://adress", "user", "pass") ;
           }
           catch (SQLException sqle) {
             System.out.println("Couldn't connect to database!");
             sqle.printStackTrace();
             return null ;
           }
           System.out.println("Successfully connected to Postgres Database");
    
           return postGresConn ;
        }
        /********************************************************/
        public static int countBooks(Connection postGresConn)
        {
          int nofBooks = -1 ;
    
          if(postGresConn == null) {
            System.out.println("We should never get here.");
            return nofBooks ;
          }
    
          Statement stmt = null ;
          ResultSet rs = null ;
          try {
            stmt = postGresConn.createStatement();
            rs = stmt.executeQuery("SELECT COUNT(*) from stud.knyga");
            rs.next();
            nofBooks = rs.getInt(1);
          }
          catch (SQLException e) {
            System.out.println("SQL Error!");
            e.printStackTrace();
          }
          finally {
            try {
              if(null != rs)
                rs.close() ;
              if(null != stmt)
                stmt.close() ;
            }
            catch (SQLException exp) {
              System.out.println("Unexpected SQL Error!");
              exp.printStackTrace();
            }
          }
          return nofBooks ;
        }
    
        public static void main(String[] args) {
           loadDriver() ;
          Connection con = getConnection() ;
          if( null != con ) {
            int nofBooks = countBooks(con);
            System.out.println("Number of books: " + nofBooks);
          }
          if( null != con ) {
            try {
              con.close() ;
            }
            catch (SQLException exp) {
              System.out.println("Can not close connection!");
              exp.printStackTrace();
            }
          }
        }
    
        }
    I get an error:
    Java Code:
    Couldn't find driver class!
    java.lang.ClassNotFoundException: org.postgresql.Driver
            at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Class.java:169)
            at _darbas.Main.loadDriver(Main.java:19)
            at _darbas.Main.main(Main.java:82)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    I downloaded:
    postgresql-8.4-701.jdbc3, the newiest driver JDBC and puted in the same folder where this code is.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You don't put the driver jar in the same folder where the code is.
    You need to run your program with the -cp flag pointing to the location (path and name) of the jar file.
    Now is a good time to start reading about classpath.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,764
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Nerijus View Post
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    Why do people always leave those generated comment lines untouched?

    kind regards,

    Jos

  4. #4
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default

    But what i can do if i am using NetBeans?

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You add the jar file to your project's build path or similar.

    The truth though is that you need to read and learn about that classpath thing.
    Your code is not going to live in Netbeans forever.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Add it to the project list of dependencies?
    There's a list of libraries the project needs...

  7. #7
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default

    Can you give me full example how i should compile this?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Netbeans does it all for you...and packages it all up nicely in a dist directory, with all relevant jars.

    All you need to do is ensure the correct jar files are part of the project.

  9. #9
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default

    Yes, now i don't have problems with this at netbeans. But I wnat to compile this at linux with comand line. I have this file:
    runSample1Jdbc.sh
    In it is code:
    #!/bin/sh
    export CLASSPATH=$CLASSPATH:/usr/share/java/postgresql.jar
    javac Main.java
    java Main
    And this have to work for me, but i don't now how to compile

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Then go and read the Oracle tutorials?
    And why do you have a script that compiles and then runs?
    Surely you only want to compile once?

Similar Threads

  1. Replies: 4
    Last Post: 07-06-2010, 10:40 AM
  2. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 06:48 AM
  3. how to store the data in data base
    By eclipse3.4ide in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 05:25 AM
  4. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 05:10 PM
  5. Connection to data base
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 06-07-2007, 05:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •