-
Java with Data Base
Can someone help me with this code:
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:
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.
-
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.
-
Quote:
Originally Posted by
Nerijus
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
-
But what i can do if i am using NetBeans?
-
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.
-
Add it to the project list of dependencies?
There's a list of libraries the project needs...
-
Can you give me full example how i should compile this?
-
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.
-
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
-
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?