Results 1 to 10 of 10
Thread: Java with Data Base
- 05-21-2010, 09:57 AM #1
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
Java with Data Base
Can someone help me with this code:
I get an error: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 downloaded: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)
postgresql-8.4-701.jdbc3, the newiest driver JDBC and puted in the same folder where this code is.
- 05-21-2010, 10:00 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 05-21-2010, 10:06 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 05-21-2010, 10:11 AM #4
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
But what i can do if i am using NetBeans?
- 05-21-2010, 10:14 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 05-21-2010, 10:29 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Add it to the project list of dependencies?
There's a list of libraries the project needs...
- 05-21-2010, 11:28 AM #7
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
Can you give me full example how i should compile this?
- 05-21-2010, 11:37 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 05-21-2010, 11:41 AM #9
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
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
- 05-21-2010, 11:54 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
How to access data at OpenOffice Base file (*.odb) from my java application
By atvienna in forum JDBCReplies: 4Last Post: 07-06-2010, 09:40 AM -
data structure and data base??
By ahmed13 in forum Advanced JavaReplies: 8Last Post: 03-27-2009, 05:48 AM -
how to store the data in data base
By eclipse3.4ide in forum New To JavaReplies: 5Last Post: 02-03-2009, 04:25 AM -
error while retrieving data from data base
By kirtesh4u in forum New To JavaReplies: 5Last Post: 11-15-2008, 04:10 PM -
Connection to data base
By Daniel in forum JDBCReplies: 2Last Post: 06-07-2007, 04:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks