This question already has answers here:
How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools? [duplicate]
(21 answers)
SQLException: No suitable driver found for jdbc:derby://localhost:1527
(19 answers)
Closed 6 years ago.
I have been looking forever how to connect to a database using java.
I Just used netbeans to create the data base, the url is
jdbc:derby://localhost:1527/project
However im using the terminal to compile and execute the java code instead of netbeans but I still have the error message that the no suitable driver found etc.. Here is the code. If anyone can help.
Thank you very much, it is really appreciated. I know that my question have been posted by I have tried everything and it dosnt work.
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class Test {
public static void main (String[] args){
String host="jdbc:derby://localhost:1527/project";
String name="groupe3";
String password= "password";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection(host,name,password);
Statement insert=conn.createStatement();
insert.executeUpdate("INSERT INTO Login VALUES('apple',2)");
insert.close();
}
catch(SQLException err){
System.out.println(err.getMessage());}
catch (ClassNotFoundException e) {
e.printStackTrace(); }
}
}
It looks like you've created a derby database, and are trying to use a mysql database driver.
https://db.apache.org/derby/docs/10.4/devguide/cdevdvlp40653.html
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?
This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 2 years ago.
package com.sm.mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.types.ObjectId;
public class JavaMongoConnection {
public static void main(String[] args) {
//System.setProperty("jdk.tls.trustNameService", "true");
MongoClientURI uri = new MongoClientURI(
"mongodb+srv://admin:admin123#cluster0-bkruu.mongodb.net/test?retryWrites=true&w=majority");
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase database = mongoClient.getDatabase("test");
}
}
This is my code, and whenever I run this code, I get the error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
I looked at java.lang.NoClassDefFoundError when using MongoDB driver and I tried their solution where I went to Run Configurations --> Dependencies and looked under Classpath Entries to make sure that I have bson-xxx.jar, mongodb-driver-xxx.jar, and mongodb-driver-core-xxx.jar listed. I have these listed and despite this, I keep getting the same error as in that stackoverflow post.
Any help would be appreciated.
com/mongodb/MongoClientURI class is present in mongo-java-driver jar. https://mongodb.github.io/mongo-java-driver/
Including the above jar in classpath should fix the issue
This question already has answers here:
Cannot load driver class: com.microsoft.jdbc.sqlserver.SQLServerDriver
(3 answers)
Closed 4 years ago.
i'm trying to establishing a connection from java to sql server. I'm using jdk 8 1.8 and sql server 2014, and this is my code:
package test.prova;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Provaconn {
public static void main(String[] args) throws Exception {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection m_Connection = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://srvdatiorim14.saga.locale;DatabaseName=mydbname", "myuser", "mypw");
Statement m_Statement = m_Connection.createStatement();
String query = "SELECT * FROM trasco_proprieta";
ResultSet m_ResultSet = m_Statement.executeQuery(query);
while (m_ResultSet.next()) {
System.out.println(m_ResultSet.getString(1) + ", " + m_ResultSet.getString(2) + ", "
+ m_ResultSet.getString(3));
}
}
but i get the following error:
Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at test.prova.Provaconn.main(Provaconn.java:10)
i've already searched for a solution in various topic, i've tried lot of various adjustment, but still i got this error.
I've also imported the jdbc driver jar, from both maven dependency and external library (i tried different version of it as you can see):
any suggestion on how to solve this? T
Thank,
Serph
EDIT - SOLVED
after changing the Class.forName into te one suggest in the answer, i also modified the url which was wrong. Changed it from
jdbc:microsoft:sqlserver://...
to
jdbc:sqlserver//...
if you use this jar file: http://www.java2s.com/Code/Jar/s/Downloadsqljdbc420jar.htm
you must change:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
to
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
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 answers here:
Connect Java to a MySQL database
(14 answers)
Closed 6 years ago.
java.sql.SQLException: No suitable driver found for jdbc:mysql#localhost:3306:emp
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.jdbd.connection.ConnectionDemo.main(ConnectionDemo.java:13)
here is my code
package com.jdbd.connection;
import java.sql.*;
public class ConnectionDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
//1. get a connection to database
Connection myconn = DriverManager.getConnection("jdbc:mysql#localhost:3306:emp","root","Dreamliner787");
//2.create a statement
Statement mystm =myconn.createStatement();
//3. Execute sql query
ResultSet myRs = mystm.executeQuery("select*from employee");
//4. process the result set
while(myRs.next()){
System.out.println(myRs.getString("last")+ "," + myRs.getString("first"));
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
The error is either because your URL is wrong, or the JDBC driver is missing.
A JDBC URL typically looks like this jdbc:mysql://localhost:3306/mysql. I'm not sure why you have an # in there. But that probably is the problem.
You can pinpoint if the problem is in the classpath by loading the driver like this.
Class.forName("com.mysql.jdbc.Driver");
EDIT :
The Class.forName isn't JDBC specific. It's simply loading the Driver class into the current class loader. Nothing related to databases there.
Prior to JDBC 4.0 you had to initialize the driver this way. I guess since this worked, you must be using a lesser version.