java - Class.forName("org.postgresql.Driver") error -
i have simple class. seek compile , run see classnotfoundexception.
import java.sql.*; public class dbprocessor{ private static string serveradres = "127.0.0.1:5432"; private static string dbname = "dota"; private static string clientname = "postgres"; private static string password = "master"; private static connection connection; public static void connect() { seek { class.forname("org.postgresql.driver"); } grab (classnotfoundexception e) { system.err.println("where postgresql jdbc driver? " + "include in library path!"); e.printstacktrace(); } seek { connection = drivermanager.getconnection("jdbc:postgresql://" + serveradres + "/" + dbname, clientname, password); } grab (sqlexception e) { system.err.println("connection failed! check output console"); e.printstacktrace(); } if (connection == null) { system.err.println("failed create connection!"); } } public static void main(string[] args){ dbprocessor db = new dbprocessor(); db.connect(); } }
i'm using windows cmd:
c:\rmi2>javac -classpath postgresql.jar dbprocessor.java c:\rmi2>java dbprocessor postgresql jdbc driver? include in library path! java.lang.classnotfoundexception: org.postgresql.driver @ java.net.urlclassloader$1.run(unknown source) @ java.net.urlclassloader$1.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.net.urlclassloader.findclass(unknown source) @ java.lang.classloader.loadclass(unknown source) @ sun.misc.launcher$appclassloader.loadclass(unknown source) @ java.lang.classloader.loadclass(unknown source) @ java.lang.class.forname0(native method) @ java.lang.class.forname(unknown source) @ dbprocessor.connect(dbprocessor.java:12) @ dbprocessor.main(dbprocessor.java:34) connection failed! check output console java.sql.sqlexception: no suitable driver found jdbc:postgresql://127.0.0.1: 5432/dota @ java.sql.drivermanager.getconnection(unknown source) @ java.sql.drivermanager.getconnection(unknown source) @ dbprocessor.connect(dbprocessor.java:20) @ dbprocessor.main(dbprocessor.java:34) failed create connection!
what i'm doing wrong? so, postgrsql library https://www.dropbox.com/s/idx5l0kub5rn1b8/postgresql.jar?dl=0
on command line, this
java dbprocessor
should be
java -cp .;postgresql.jar dbprocessor
and, because includes java.sql.driver
, when have working remove
// seek { // class.forname("org.postgresql.driver"); // } grab (classnotfoundexception e) { // system.err.println("where postgresql jdbc driver? " // + "include in library path!"); // e.printstacktrace(); // }
per drivermanager
javadoc,
the drivermanager
methods getconnection
, getdrivers
have been enhanced back upwards java standard edition service provider mechanism. jdbc 4.0 drivers must include file meta-inf/services/java.sql.driver
. file contains name of jdbc drivers implementation of java.sql.driver
. example, load my.sql.driver
class, meta-inf/services/java.sql.driver
file contain entry:
my.sql.driver
applications no longer need explictly load jdbc drivers using class.forname()
. existing programs load jdbc drivers using class.forname()
go on work without modification.
java jdbc
No comments:
Post a Comment