There are a lot of questions: "How to start working with MySQL as an embedded database?", "How to use Connector/MXJ", etc. But there is no any useful information (neither tutorials!). I mean there is no detailed instructions how to do such things. Of course, there is a MySQL website, where is an article about using MysqldResource. Actually, I don't understand what it is.
Let's finish this lack of any restraint! Please, if you are experienced in this topic, give as full instruction as you can! (what to download, how to add jars(say, to eclipse), some code will be great...)
For example, the following code doesn't work - ClassNotFoundException- though I have added mysql-connector-mxj-gpl-5-0-11.jar and mysql-connector-mxj-gpl-5-0-11-bd-files.jar to the project classpath.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseWorks {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost", "root", "");
Statement st = con.createStatement();
String query = "SELECT VERSION();";
ResultSet rs = st.executeQuery(query);
rs.next();
System.out.println("success!!!! " + rs.getString(1));
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
For example, the following code doesn't work - ClassNotFoundException- though I have added mysql-connector-mxj-gpl-5-0-11.jar and mysql-connector-mxj-gpl-5-0-11-bd-files.jar to the project classpath.
This is a basic Java problem. Here's what I'd do to solve it:
Examine the stacktrace to find out which class is missing.
Use jar tvf ... to list the contents of JAR file(s) you think the missing class should be in. Is it there? Is the name / package correct?
If it is there, you've got the application's runtime classpath wrong.
If it is not there, you are missing a JAR file. Go back to the documentation and read it again.
(If you showed us the stacktrace, and told us how you are building and launching your code, perhaps we could be a bit more specific ...)
More informations here:
http://dev.mysql.com/doc/refman/5.1/en/connector-mxj.html
http://jroller.com/mmatthews/entry/yes_it_really_is_this
Related
You can check the in the image(link at the end of sentence) the library and code as well ---
The error is java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. I already have mysql-connector-java-5.1.23-bin and it's already pasted in my Webserver's lib directory. Below is the code.
package databaseinsertexample;
import java.sql.*;
import java.sql.DriverManager;
public class DatabaseInsertExample {
public static void main(String[] args) {
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myteam","root","");
String sql="insert into teams values(?,?,?)";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setString(1,"India");
stmt.setString(2, "Niraj");
stmt.setInt(3, 100);
stmt.execute();
con.close();
} catch(Exception e)
{
System.out.println(e);
}
}
}
On the Localhost site of XAMPP, the Database has been created properly but when I'm executing the program on my Java file via Netbeans it's not reflecting on the Database. Please help I'm new to this Programming world, I checked on YouTube still I'm not able to clear this doubt of mine.
change your clase forName like this.
Class.forName("com.mysql.jdbc.Driver");
And also please refer this question - How to connect XAMPP MySQL local DB using JDBC?
For the past couple of days I have been trying to get my Derby database to be accessed in my jar file. Here is what my connection class looks like:
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class DBConnection
{
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String JDBC_URL = "jdbc:derby:EmployeeInfo";
public static Connection dbConnector()
{
try
{
Class.forName(DRIVER).newInstance();
Connection conn = DriverManager.getConnection(JDBC_URL);
JOptionPane.showMessageDialog(null, "Connection successfull");
return conn;
}catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
Also, here is a screenshot of what my project explorer looks like:
Everything works when I am in eclipse, my program runs as expected with my GUI updating the Database. But the minute I make my program a jar file it says it can't find my database EmployeeInfo (note: Database.jar is the EmployeeInfo database). One last thing, when I try the jar file out on a different machine it also states that it cannot find the database.
An explination on why this is going along with any fixes would be great!
-Thanks,
Aaron :)
Well i eventually found the fix :). I had to place the embedded derby database files into a folder along with the derby jars, and my jar file that contained my project and everything worked beautifully!
This question already has answers here:
What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?
(15 answers)
Closed 6 years ago.
Got error while I'm tried to connect SQL database
using cmd. Here is my program.I use jdk 6 version to compile and run
Thanks in Advance.
import java.io.*;
import java.sql.*;
class Dbs
{
public static void main(String args[]) throws Exception
{
try
{
Connection con = null;
Statement s = null;
ResultSet rs = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String bala = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\ss.mdb";
con = DriverManager.getConnection(bala,"","");
s = con.createStatement();
rs =s.executeQuery("select * from Table1");
while(rs.next())
{
System.out.println("Name"+rs.getString("name"));
System.out.println("No"+rs.getString("num"));
}
}
catch(Exception e)
{
System.out.print(e);
}
}
}
Error:
Are you in the correct directory ? It seems you are in your jdk directory, where you should be in your program's directory (where your Dbs.class resides).
Also, you missed the public keyword. Here, your Dbs is package local, so it won't be visible outside the package. Depending on where you use it, it may trigger the error.
Try:
public class Dbs {
// code
}
Also,
If you don’t explicitly specify a package, your classes and interfaces end up in an unnamed packaged, also known as the default package. Best practice is not to use the default package for any production code.
more here.
the error says the class you are trying to get is not where it must, check the the jdbc driver is in the correct place ,check if you have your JAVA_PATH set and put a try catch to see if there is another error who is causing that
I'm having difficulties in Java with an SQLITE database provided in a separate JAR file.
Surprisingly, the sqlite database seems to be accessed even after removing the JAR file, exiting and restarting the program and even after rebooting the machine.
I'm using the Xerial driver sqlite-jdbc-3.7.2.jar (for org.sqlite.JDBC).
EDIT: very same issue with sqlite-jdbc-3.8.6.jar.
Xerial JDBC driver is published here:
https://bitbucket.org/xerial/sqlite-jdbc
I'm really puzzled. Is there some kind of persistent cache for this particular JDBC driver? or is it something I missed regarding JDBC in general?
CODE SAMPLE:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqliteJDBCTest {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite::resource:jar:file:doesntexistJAR.jar!/doesntexistDB.sqlite");
System.out.println("connection = " + connection);
statement = connection.createStatement();
System.out.println("statement = " + statement);
rs = statement.executeQuery(" SELECT * FROM nonexistentTable WHERE key = 'nonexistentKey'");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
The above code sample shows the first step of the problem: on the first run, DriverManager.getConnection(..) throwed an exception as expected:
$ java -jar sqliteJDBCTest.jar
java.sql.SQLException: failed to load jar:file:doesntexistJAR.jar!/doesntexistDB.sqlite: java.io.FileNotFoundException: doesntexistJAR.jar (Aucun fichier ou dossier de ce type)
at org.sqlite.Conn.open(Conn.java:92)
at org.sqlite.Conn.<init>(Conn.java:57)
at org.sqlite.JDBC.createConnection(JDBC.java:77)
at org.sqlite.JDBC.connect(JDBC.java:64)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at SqliteJDBCTest.main(SqliteJDBCTest.java:18)
But, since then, for each run I get the following output:
>java -jar sqliteJDBCTest.jar
connection = org.sqlite.Conn#3fee733d
statement = org.sqlite.Stmt#5acf9800
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: nonexistentTable)
at org.sqlite.DB.newSQLException(DB.java:383)
at org.sqlite.DB.newSQLException(DB.java:387)
at org.sqlite.DB.throwex(DB.java:374)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:123)
at org.sqlite.Stmt.executeQuery(Stmt.java:121)
at SqliteJDBCTest.main(SqliteJDBCTest.java:23)
In this example, the SQLException "SQL error or missing database" is not the error we're expecting!
Not only is the database missing, but even the JAR file supposed to contain it!
So how come getConnection() doesn't throw an exception in the first place?
Short answer: because of a bug in the Xerial JDBC driver.
When one requests the Xerial driver a connection by calling DriverManager.getConnection(jdbc:sqlite::resource:jar:file:<local_location_of_JAR>!/<name_of_database_file>), the driver creates a copy of the database file on the temporary directory specified by the Java system property java.io.tmpdir and then operates on this copy.
The problem is that when the original JAR is removed, the driver loads the copy the next time(s) one uses the same getConnection() call. For me, this is a bug; the driver should at least check that the (possibly remote) JAR file pointed to by the URL is still there...
Second problem (the one described in the CODE SAMPLE of this post): when the original JAR file doesn't exist, a "copy" of the (non-existent) database is created, and the next time(s) getConnection() is called with the same parameters, the driver directly returns a phantom connection to this empty database that was never found...
I submitted this story in a bug report on the Xerial JIRA website:
https://bitbucket.org/xerial/sqlite-jdbc/issue/158/drivermanagergetconnection-not-returning
So I just started learning about databases this week and one of the things I need to be able to do is connect to my mySQL database that I created using Java. I have done some research and I have tried to find the correct way of doing this I just can't seem to figure out how. Here is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Menu
{
public void menu()
{
Connection conn;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "gym";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}
catch (Exception e)
{
System.out.println("NO CONNECTION =(");
System.out.println(e.getMessage());
}
}
}
Now the problem is, is that every time I run this code, the "No Connection =( " appears and then it says the error is: "com.mysql.jdbc.Driver". Can somebody please help me and say what I am doing wrong? Thank you. Much appreciated.
Your error means that your library path doesn't contain the jar that contains the com.mysql.jdbc.Driver class.
You don't have to change anything in your code. If you are running it via Eclipse, you should add mysql-connector-java-x.x.x-bin.jar to your build path (where x.x.x is the version of the jar).
All JDBC connection classes need their respective drivers which are normally supplied as jar files from the database vendor, add the relevant database driver to your classpath.
The .jar file will be available from the vendors site, in this case : http://www.mysql.com/products/connector/ and then to add this to the classpath of your project in the ide of your choice. Here is a guide for eclipse : http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)
Try and run again once you have this.