thing used : eclipse, java 1.8.0 version, mariadb 10.2, mariadb-java-client-2.4.1jar
problematic point : I have only used oracle db until now, but it is my first time using maria db.
Integrated with db in eclipse>Util.java. However, when you create a jsp page and enter data(Data query results ("select * from..." sql statement with db)),
the [java.lang.ClassNotFoundException:org.mariadb.jdbc.Driver] error appears.
The jdk version I'm using is 1.8, so I have to use 52.0,
but I don't know what to Whether to use. I've already used 2.x among jar versions. But no. I'd appreciate it if you could help me solve it.
my Util.java code
package DBPKG;
import java.sql.*;
public class Util {
public static Connection getConnection() throws Exception {
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mariadb://localhost:3306/board",
"root",
"1234");
return conn;
}
public static void main(String[] args) throws Exception {
System.out.println(Util.getConnection());
}
}
Console Results
org.mariadb.jdbc.MariaDbConnection#58fdd99
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?
import java.sql.*;
class MySQLconn
{
public static void main(String args[]) throws SQLException, ClassNotFoundException{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","system","12345");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from emp");
while(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("Name")+" "+rs.getString("Department"));
}
rs.close();
st.close();
con.close();
}
}
Apparently, the Oracle JAR's are not in your classpath. A trick to make sure they are at compile time is to import the OracleDriver class and then replace the string literal with OracleDriver.class.getName(). Your IDE or the compiler will notice if the class is not available.
Obviously, you need to get the Oracle JAR's and put them on your classpath.
from your exception It seems you dont have Oracle Database Driver in your project classpath, you can download one in here oracle jdbc driver
If you using ecelipse IDE you can add the driver by following these steps :
Right click on your project folder
Choose build path > configure build path
switch to Libraries tab, then choose Add External Jars
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
This question already has an answer here:
Manipulating an Access database from Java without ODBC
(1 answer)
Closed 6 years ago.
I want to connect Java 8 with Access but the following error occurs and I don't know how to fix it. I always get this error:
java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:/Users/Ghazi/workspace/java w access/login.accdb
I added 4 libraries:
hsqldb.jar
jackcess-2.0.7.jar
org.apache.commons.lang-2.6-source.jar
org.apache.commons.loggin-1.1.1-source.jar
This is my code
import java.sql.*;
public class DbConnection {
Connection con;
Statement st;
DbConnection(){
dbconnect();
}
//-----------------------
public void dbconnect(){
try
{
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Ghazi/workspace/java w access/login.accdb");
Statement stment = conn.createStatement();
}
catch(Exception err)
{
System.out.println(err);
}
}
//--------------------------
public static void main(String[]args){
DbConnection ob=new DbConnection();
}//end main
}
Try adding "Class.forName():
public void dbconnect(){
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Ghazi/workspace/java w access/login.accdb");
Statement stment = conn.createStatement();
}
catch(Exception err) {
System.out.println(err);
}
}
The basic problem is that earlier versions of Java/JDBC used ODBC to connect to MS-Access ... and the ODBC driver has been removed from Java 8.
Two alternatives are to use:
1) UCanAccess: http://ucanaccess.sourceforge.net/site.html
... or ...
2) Jackcess (Jackcess 2.0: New crunchy outside, same yummy filling!): http://jackcess.sourceforge.net/
If that doesn't work:
3) Please specify what what IDE you're using (Eclipse, etc - if applicable)
4) Make sure your jackcess-2.0.7.jar is explicitly included in the CLASSPATH (how to do this depends on your IDE)
5) Consider using a directory without spaces in the name
I added 4 libraries
You need five (5) libraries. You forgot to add the "ucanaccess-x.y.z.jar" file itself.
For detailed instructions, see
Manipulating an Access database from Java without ODBC
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