Sunday, August 2, 2009

Java database question: Could not Load JDBC driver !!! I am trying to hook up my database with the java and?

i am using a driver named ojdbc14.jar i copied it into C:\Program Files\Java\jdk1.6.0_03\


jre\lib\ext





and i unzipped it over there as well to make sure nothing is missing;


when i run the code this is what i get:





C:\Nik%26gt;java MainScreen


Could not Load JDBC driver


java.sql.SQLException: Io exception: Invalid connection string format, a valid f


ormat is: "host:port:sid"


Exception in thread "main" java.lang.NullPointerException


at MainScreen.%26lt;init%26gt;(MainScreen.java:84)


at MainScreen.main(MainScreen.java:217)








can anyone please tell me what's wrong ? if driver is the problem, where can i get a driver and how can i install or where should i copy it.





your help is greatly appreciated

Java database question: Could not Load JDBC driver !!! I am trying to hook up my database with the java and?
A. "i unzipped it over there as well to make sure nothing is missing" + "Could not Load JDBC driver"








i%26gt;Don't unzip ojdbc14.jar. Jar files must be kept as jar files.





ii%26gt;Check your CLASSPATH. May be ur system is actually using some other JRE. Possibly there's one at C:\Windows\System....





iii%26gt;Set the classpath manually to





.;c:\Program Files\Java\jdk1.6.0_03\jre\lib;c:\Progra... Files\Java\jdk1.6.0_03\jre\lib\ext\ojdbc...





B. "java.sql.SQLException: Io exception: Invalid connection string format, a valid format is: "host:port:sid"





This clearly indicates problem with Connection URL format. Read the JDBC Driver documentation for correct format of Connection URL. From the Error message it seems that the URL should be "jdbc:host:port:sid"





Then check ur hostname/ IP Address, the port no. on which Oracle listnes, and the database SID. Default SID for oracle is "orcl".


Form the correct Connection URL and use it in ur code.
Reply:The Driver I use is connector/J (www.mysql.com/products/connector-j). It's pretty strait forward to download (I recommend getting version 3.1.14 as any later version seems to have issues). Once you download the connector, you must include the "mysql-connector-java-3.1.14-bin.jar" file in your project. To load the database connection I use something like....





public boolean initializeConnectionWithDatabase (String sqlIP, int sqlPort, String databaseName, String user, String pass){


boolean success = false;


try {


// Load the JDBC driver


String driverName = ""org.gjt.mm.mysql.Driver";


Class.forName(driverName);





// Create a connection to the database


String url = "jdbc:mysql://" + sqlIP + ":" + sqlPort + "/" + databaseName; // JDBC url


connection = DriverManager.getConnection(url, user, pass);


success = true;


} catch (ClassNotFoundException e) {


System.out.println("Could not find the database driver");


} catch (SQLException e) {


System.out.println("Could not connect to the database --%26gt; "+e.getMessage());


}


return success;


}





"connection" is a Connection class object found in java.sql or javax.sql. Experiment with it a bit, it should be fairly easy to get it working properly.


Hope this helps.
Reply:you must put the jdbc Driver on you path


there are two ways:


1) from your command prompt:


c:\%26gt; set path=C:\Program Files\Java\jdk1.6.0_03\jre\lib\ext;%path...


in this way you must do this action all times you want to run your program.


2) right click on my computer choose properties then from above tabs choose advanced then enviroment variables define new user variable by clicking new, set variable name "path" and the value "C:\Program Files\Java\jdk1.6.0_03\jre\lib\ext" then save it o close all dialogs then close all command prompts open a new command prompt


in this way you solve the problem for ever

survey research

No comments:

Post a Comment