Can anyone please tell me as to how to setup the path for Connector/j using the "mysql-connector-java-5.1.18-bin.jar" in windows 7?
I'm using the below code and it always end up throwing an exception.
(java.lang.ClassNotFoundException : com.mysql.jdbc.driver)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class LoadDriver
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Connection working");
}
catch (Exception ex)
{
System.out.println("Connection Fail");
System.out.println(ex.getMessage());
}
}
}
I tried following the official documentation of connector/j for setting up the path, but could not understand.
I tried adding E:\PROGRAM FILES\JAVA\jdk1.7.0_01\jre\lib\ext\mysql-connector-java-5.1.18-bin.jar in the "path" environment variable, Please correct me.
First, you dont need newInstance on Class.forName("com.mysql.jdbc.Driver")
Second don't copy jars to your JDK folder, there shouldn't ever bee a need to copy them there. The correct thing to do is add the jar to your project as a dependency. If your not using an IDE, then you want to add the jar to java.exe as a --classpath option when you run your code (run "java.exe /?" for more details). If you're using eclipse, you should add the mysql jar to the project by clicking on the project and selecting "properties" and then "Java Build Path" there will be an "Add JARs..." button on the right. Then the IDE will add it to your classpath automatically.
Related
I am having some difficulty in making connectivity with Java and PostgreSQL Database.I have download the JDBC4 Postgresql Driver, Version 9.2-1002 driver and properly set the application ClassPath. My code is as under
import java.sql.*;
public class JavaPostGreSQLConnectivity
{
public static void main(String[] args)
{
DB db = new DB();
db.dbConnect("jdbc:postgresql://127.0.0.1:5432/TestDB", "postgres","pwd");
}
}
class DB
{
public DB() {}
public void dbConnect(String db_connect_string, String db_userid, String db_password)
{
try
{
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
System.out.println("connected");
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
Upon running I am getting the below error
Is it complaining about
Class.forName("org.postgresql.Driver");
If so then what will be the driver name? However, I followed this for my learning purpose.
However, If I do
C:\Program Files (x86)\Java\jdk1.7.0\bin>java -cp C:\Users\pos
tgresql-9.2-1002.jdbc4.jar; JavaPostGreSQLConnectivity
connected
It works.Why I need to explicitly mention the driver again when I have already placed it in the classpath properly? Is there any alternative way (I just want to put the JAR file in Classpath and the program should read from there)?
Thanks in advance
The driver name is OK. It is the same as mentioned in the official docs of the driver. Therefore the driver is just not in the classpath.
You say:
I [...] properly set the application ClassPath
On the other hand you start the program by just calling:
java JavaPostGreSQLConnectivity
In that case no PG driver is on the classpath. You have to add it by hand using someting like
java -cp postgresql-jdbc4.jar JavaPostGreSQLConnectivity
EDIT The question has been changed while typing, hence the duplication.
You added the jar only in you IDE. This helps the IDE to compile your code. If you start the program using you IDE then the IDE will also set the classpath for you. But if you don't start via the IDE then nobody knows the correct classpath and it has to be set by hand.
Your options are:
start always via IDE
make some batch script which hides the setting of the classpath (common solution)
set the CLASSPATH environment variable (does not scale with other Java applications)
make an "Executable Jar" and set the classpath there. (Search this site using that term).
put the jar into a place where the JVM picks it up automatically (e.g. in the lib/ext directory of the JRE). But polluting the JRE/JDK libs is the worst option.
Note: This is all basic Java knowledge and has nothing to do with PostgreSQL.
package aplikacjajava;
import java.sql.*;
public class main
{
public main( String args[] )
{
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:projekt.db");
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
Error:
java.lang.ClassNotFoundException: org.sqlite.JDBC
I know that I'm missing JDBC driver. How to install it on Fedora?
I don't how to add some "path" and what is this at all. I need something like step-by-step explanation... This is first time when I met with such a problem.
Download the SQLite-jar at http://search.maven.org/remotecontent?filepath=org/xerial/sqlite-jdbc/3.8.10.1/sqlite-jdbc-3.8.10.1.jar
Add the jar file to your class path (open Eclipse project properties, there you'll find the build path)
Run your program.
You usually use Maven, Gradle or manual dependency management when using Java. Linux package managers are very limited in java support.
After I set the classpath from the command prompt, I could use the Database by running java javafile.java, and the database was working fine only from the command prompt.
The Eclipse IDE does not recognize the class. While I tried to add external jar file, the good news was when I defined the classpath as a variable inside the Eclipse IDE, then the issue was solved completely.
I did notice your request and tried to add well-detailed instructions on how to fix that issue.
For also solving this issue, follow the steps below:
Download a sqlite-jdbc-XXX.jar file
in the Eclipse IDE from the left side, right-click on your project and then select properties
on the left menu, choose the Java build path and click Add variable
click Configure variables and then click New
you should name the variable name field as you name it on the Class.forName("org.sqlite.JDBC"); line, that means your variable name is: org.sqlite.JDBC
on the variable path, you should choose your sqlite-jdbc-XXX.jar file
Finally, I got results from the database also in the Eclipse IDE.
This is a code i tried for common Connection class jdbc project but it returns only null
package dbDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionDemo {
Connection con;
public Connection getConnection() {
try
{
Class.forName("com.mysql.jdbc.driver");
String url="jdbc:mysql://localhost:3306/myserver";
String name="root";
String pwd="admin123";
con=DriverManager.getConnection(url,name,pwd);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return con;
}}
anybody can please help this code right or wrong.
Please change the driver class to
com.mysql.jdbc.Driver
Then you should have the mysql-connector.jar in your classpath.
If it is an eclipse ide just right-click on the project->build path->configure build path->go to libraries that->add external jar->select the mysql-connector.jar from the place you kept after downloading it.
the best way to know the exact error you have to use e.printStackTrace() instead of System.out.println(e.getMessage()) and then execute again it will print all the errors which help you to debug more
Download JDBC jar from here: http://dev.mysql.com/downloads/connector/j/5.0.html and make sure that you include it to Java's CLASSPATH when you run your application.
After you unpack a downloaded zip or tar file, you'll need only one file to make it working: mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar
After extracting the JAR file you can run your program like this:
java -cp path-to-mysql-connector-java-5.0.8-bin.jar your-java-class
The suggestion about changing 'driver' to 'Driver' in the driver's class name is also correct.
i have jogl in the path and this program works fine until i include "import net.java.games.jogl.*;" on the top of the code. I get error "Import cannot be resolved" As i told you it works without the import. The Native Libraries are installed and jogl.jar is installed. Why isnt Eclipse recognizing this package import? here is the code:
import net.java.games.jogl.*;
public class HelloWorld
{ // open HelloWorld
public static void main(String[] args)
{ // open main
try
{ // open try
System.loadLibrary("jogl");
System.out.println("Hello World! (The native libraries are installed.)");
} // close try
catch (Exception e) // all try's need a catch
{ } // even if the catch does nothing
} // close main
} // close HelloWorld
It appears that the example you are working from is using an outdated package name. Take a look at the contents of the jar to determine the correct package.
This link (from 2006) is suggesting you may want to look at the package javax.media.opengl. Also, here is some javadoc for jogl that I found. I am not sure how up to date it is though...
I am not sure if you are using Maven or not but check your Java Build Path from inside Eclipse. Under Libraries tab ensure that JOGL is listed.
After reading up on some options (sqlite, derby etc...), I've decided to throw down with HSQLDB. I've downloaded it, read up on it and followed a 'hello world' type intro to it, and am now stuck.
I believe that you have to put the hsqldb.jar file in the src folder, so I did exactly that. Then I made a reference to the package with Eclipse by going into Run -> Run Configurations, then going into the Classpath tab, then clicking User Entries, then add External Jar, and selecting hsqldb.jar.
I get this :
java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver
Here's my code :
package mysqlite;
import java.sql.*;
public class myclass {
public static void main(String[] args) {
try {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:db";
String user = "aUser";
String password = "";
Connection conn = DriverManager.getConnection(url, user, password);
}
catch(Exception e) {
System.out.println(e.toString());
}
}
}
I understand it's unable to find a class, but I thought that was what the hsqldb.jar provided.
No, you do not have to put it into src folder. src is for source files (*.java). You have to add this jar into your classpath: click on project properties, choose "Java build path", select tab "Libraries" and add the jar here.
The jar can be stored anywhere in your file system. Sometimes people create lib directory under project home and put all 3rd party dependencies there.
Try putting the .jar here:
<YOUR_JAVA_HOME>\jre\lib\ext