I have downloaded the following:
mysql-essential-5.1.65-win32 from this MySQL Dev link
MySQL Connector mysql-connector-java-5.1.21.zip from this link
Now I have started programming with Eclipse. I have made simple java class like below,
public class MySQLAccess {
private static Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public static void main(String[] args){
try{
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
}catch (Exception e) {
// TODO: handle exception
System.out.println("Error : "+e);
}
}
}
I have also made a folder "lib" in my Java project and I have put that mysql-connector jar over there. But when I run this program it can't find mysql I get the following error in the console :
Erro : java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Can someone please tell me where I have made the mistake? Thank you
Putting the full jar-file path in your classpath and restarting cmd (if you are running from cmd) should work-
See here- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
If it your java application project then you put jar file into jdk library path ext folder.
eg:
C:\Program Files\Java\jdk1.6.0_13\jre\lib\ext
you will get path from your project class path .
Related
On Mac, installed Java 12.0.2, postgresql-42.2.8.jar, PostgreSQL 11.5. From what I can tell, that version of the JDBC driver should be compatible with that version of PostgreSQL.
I've tried putting the PG jar file in my /Java/Extensions directory, and in the same directory as the following test program, and even set CLASSPATH to my test program directory, but I'm still getting ClassNotFound at runtime for the line Class.forName("org.postgresql.Driver"); in my test program:
import java.sql.Connection;
import java.sql.DriverManager;
public class PostgreSQLJDBC {
public static void main(String args[]) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/testdb",
"postgres", "123");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
The test program is taken directly from: https://www.tutorialspoint.com/postgresql/postgresql_java.htm
Any help/suggestions much appreciated... thanks.
Your classpath probably needs to explicitly reference the postgresql jar file. That's what I've always done. There might be some magic associated with putting jars in a particular directory, but I'm 99% sure that referencing a random directory in the classpath won't traverse .jar files, only .class files.
I am new in java Jdbc I am going to write simple jdbc program `
import java.sql.*;
class jdbcDemo
{
public static void main(String[] args) throws Exception
{
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","Scott","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from rk");
while(rs.next())
{
System.out.println(rs.getInt(1)+"---"+rs.getString(2)+"---"+rs.getInt(3));
}
con.close();
}
}
after Successful compiling program
At runtime i found following error
I had used editplus editor
and also i had created rk table in oracle 12c database
need help to fix issue
You need to provide the class for oracle driver. It will be in a jar which needs to be in class path. Since you are running from command prompt, this needs to be passed to run the class successfully.
As you load class of oracle driver in your code :
Class.forName("oracle.jdbc.OracleDriver");
you just need to add oracle jdbc drivers(ojdbc6 or ojdbc7 ) into your project .
you can download this jar file from thislink :
JDBC DOWNLOAD LINK
Currently trying to use a sqlite-dbc4-3.8.2-SNAPSHOT.jar that was given to me as part of an assignment. I've tried running my main file and I get the errors below:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
org.sqlite.JDBC cannot be resolved to a variable
at DbBasic.open(DbBasic.java:54)
at DbBasic.<init>(DbBasic.java:67)
at DbUser.<init>(DbUser.java:40)
at Main.go(Main.java:12)
at Main.main(Main.java:65)
Here's part of the DbBasic class that attempts to connect and open a database using JDBC:
private Connection getConnection()
// get the connection
{
Connection con = null;
try {
con = DriverManager.getConnection(SQLITE_DATABASE_LOCATION+dbName);
con.setAutoCommit(false);
}
catch (SQLException sqle)
{
notify("Db.getConnection database location ["+SQLITE_DATABASE_LOCATION+"] db name["+dbName+"]", sqle);
};
return con;
} // end of method "getConnection"
private void open()
// "open" the database : actually really setting up the connection and obtaining the metadata about the server
// makes sure that database file is present before trying to establish connection
// otherwise SQLite will create a new, empty database with the name provided
{
File dbf = new File(dbName);
if (dbf.exists() == false)
{
System.out.println("SQLite database file ["+dbName+"] does not exist");
System.exit(0);
};
try {
Class.forName(org.sqlite.JDBC);
con = getConnection();
} catch ( ClassNotFoundException cnfe ) {
notify("Db.Open",cnfe);
};
if (debug) System.out.println("Db.Open : leaving");
} // end of constructor "Open"
I have already tried adding external JAR's and the .jar file is then added to my 'Referenced Libraries' in Eclipse.
I'm having trouble understanding the Class.forName(org.sqlite.JDBC) and how to make it work with my .jar file
Though the post is relatively old and the OP might not be interested in a solution for this issue now anymore, thought of putting this as an answer. It might help someone who runs into such silly issues. This is one of the common mistakes that people do while using Eclipse as an IDE, try to run code that doesn't even compile.
You can check the "Problems" view in Eclipse and fix the compilation errors and then try to compile your program. The obvious error here on this question is the missing double quotes "" while using the driver name.
Class.forName("org.sqlite.JDBC");
As you can see that I am in the current path of executing the java file.
When I compile the program I am getting a class file successfully.
but wen I run it I m unable to execute that program as I am getting the error in the image telling that there's no such class in that path available..
C:\Users\admin>javac insertImg.java
C:\Users\admin>java insertImg
Exception in thread "main" java.lang.NoClassDefFoundError: insertImg
Caused by: java.lang.ClassNotFoundException: insertImg
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: insertImg. Program will exit.
C:\Users\admin>
And this is my java code for inserting image into database.....
insertImg.java:
import java.sql.*;
import java.io.*;
public class insertImg{
public static void main(String[] args) {
System.out.println("Insert Image Example!");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName = "hibernatetutorial";
String userName = "root";
String password = "root";
Connection con = null;
try {
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
Statement st = con.createStatement();
File imgfile = new File("images.jpg");
FileInputStream fin = new FileInputStream(imgfile);
PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
pre.setInt(1,5);
pre.setString(2,"Durga");
pre.setBinaryStream(3,fin,(int)imgfile.length());
pre.executeUpdate();
System.out.println("Inserting Successfully!");
pre.close();
con.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Does insertImg.java have a class insertImg (note case sensitivity), that has a public main function?
I see You got the code from roseIndia it is working for me without any hitch. however its still weird from whatever your showing ..my suggestion is to use an IDE like eclipse
you can always try the following steps
1) Create a table in your Mysql database to suit the above code
CREATE TABLE IMAGE
(
IMG_ID INT,
IMG_NAME VARCHAR2(100),
IMG BLOB
)
2) Create a new java project in eclipse and create a new class insertImg and paste the code there. please make sure your image file path is same as where your image is and run it. Its working fine
Ive tried with Oracle database had to add the -classpath ojdbc.jar and it worked from the console also as you can see in the image
and my image file that i loaded in the database is in D:\ so its is not in the above screenshot
hope this helps
This is a simple classpath error. The CLASSPATH environment variable allows Java to know where to look for classes.
Unfortunately, Java is not intelligent enough to know that it needs to look for classes in the local directory. To resolve this issue, all you have to do is execute one of the following statements depending on whether you are using windows or Linux/Unix:
For Windows:
set CLASSPATH=.;%CLASSPATH%
For Linux:
export CLASSPATH=.:$CLASSPATH
I have Eclipse Indigo on Mac Os X and I have downloaded mysql connector (version 5.1).
I have added the jar to the project and I am using this code:
public class Test
{
public static void main (String[] args)
{
try {
String url = "jdbc:msql://200.210.220.1:1114/Demo";
Connection conn = DriverManager.getConnection(url,"","");
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
while ( rs.next() ) {
String lastName = rs.getString("Lname");
System.out.println(lastName);
}
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
When I try to execute the program I get this exception:
Got an exception!
No suitable driver found for jdbc:msql://200.210.220.1:1114/Demo
My question is : how to install the driver? What else should I do?
And once installed the driver, how do I get my database URL (I am using mysql 5.5)?
I haven't found a valid guide on the web, they're all too specific.
Your JDBC connection URL is not correct, refer to the official documentation to check the required format for the URL .
In your case the URL will become :
String url = "jdbc:mysql://200.210.220.1:1114/Demo";
you're missing the "y" in jdbc:mysql
You are using MySQL, the URL should look like this:
jdbc:mysql://200.210.220.1:1114/Demo
may be, review the IP and PORT.
You may have added the jar to your project but have you also added it to the project classpath? Having the jar exist as a file in your project won't solve the problem. The jar file is clearly not accessible to your program. Right click on your project -> Build Path -> add the jar there.
Database URL looks ok, assuming you have the right host address and port number.