This question already has answers here:
Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0
(11 answers)
Closed 6 years ago.
How do I install MSSQL driver in my project with Eclipse's maven? (m2e)? And also make it not conflict with Vaadin? When I install the MSSQL driver locally following these instructions, during the compile and runtime, it says "Could not find archetype from Vaadin-addons."
I have the code:
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://server;databaseName=dbname;user=username;password=password");
String sql = "Select * from Table1";
stmt = con.createStatement( );
rs = stmt.executeQuery(sql);
while (rs.next()){
contractorsList.addBean(new Contractor(rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6)));
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}finally{
try { con.close(); } catch (SQLException e) {}
try { rs.close(); } catch (SQLException e) {}
try { stmt.close(); } catch (SQLException e) {}
I'm getting the following error:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:450)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:403)
at java.lang.Class.forName0(Native Method)
Which means that it can't find my driver, I think. So I'm following this tutorial on installing the MS SQL driver. I download the driver from microsoft and then I unzip it here:
C:\SQLDriver
In Eclipse, on my project folder, I right click > Debug As > Debug Configurations >
mvn install:install-file -Dfile=C:\SQLDriver\sqljdbc_4.0\enu\sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0.2206.100 -Dpackaging=jar
Then I add in the following in my POM
<properties>
<!–….other versions–>
<sqlserver.version>4.0.2206.100</sqlserver.version>
</properties>
...
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>${sqlserver.version}</version>
</dependency>
I get an error that it can't find my dependency, So I look up the question here, and I found out that I simply need to add sqljdbc4 to my classpath.
I have no idea what that means but google took me here So I attempt to follow the instructions.
In System Properties > Advanced > Environment Variable under Variables, I add the following:
I searched here and it looks like someone else asked the same question
I honestly don't know what people mean by add it to the classpath. Do they mean paste the jars in the same folder as the project? Either way, I just found a .classpath file in my project and added these:
How do I get maven to use this microsoft sql driver?
[Edit] When going to Libraries > Maven Dependencies, right click > build path > configure build path, I see this:
Which, when I click, it doesn't let me edit the path of that file. I'm not sure where to edit it.
You will need to download the source code and the java docs for all the maven dependencies. You can do that by right clicking the maven as discussed here:
https://stackoverflow.com/a/22352526/1475228
Then only you can run the code.
Also look here. This is the one that helped mine.
Related
I'm new to java and I am using Visual Studio Code for making a Java project. I'm trying to write SQL queries after loading driver in Visual Studio Code, but I'm repeatedly getting SQLException. Here is my project folder:
src
-com/folder
-db
-DBConnection.java
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(path, username, password);
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
return false;
}
Every time I run, I get the following error
Exception in thread "main" java.sql.SQLException: No suitable driver
found for jdbc:mysql:localhost:3306/assign
I'm getting correct results when I run the same program in the src folder.
The most common reason for this type of error is missing updated jar in classpath folder.
If you are using latest version of jdbc jar make sure it correctly points to classpath where the jar is located.
Hint: put some debug statement before invoking of class so that you can be sure correct jar is invoking everytime.
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 2 years ago.
When I run my code, I get this error: Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver; with a stack trace link to line 13 (Highlighted in the code)
I have just started using JDBC and don't know much about it, but I'll go through what I have done so far to get to where I am. As a preliminary, I am using MySQL Workbench and Apache Netbeans 11:
1) Downloaded the .jar connector file
2) Could not find the build path on netbeans 11 and did some research and couldn't find any resources linking to it, so instead used the Driver dropdown through the databases section on the services tab. Now I can see all my SQL databases and tables in my netbeans IDE.
3) Wrote the following code using the 7 steps to connect to the database, establish a connection etc
import java.sql.*;
public class GroundControlToMajorTom {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306//customers";
String uname = "root";
String pass = "";
String query = "SELECT customer_id FROM customers WHERE customer_id = 1";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, uname, pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
rs.next();
String id = rs.getString("customer_id");
System.out.println(id);
st.close();
con.close();
}
}
4) Run the code and get a ClassNotFoundException. I did a bit of research and it seems to say that I don't have the connection to the actual driver, but I added it in the drivers section of the services for my project?
Any help would be much appreciated my dudes <3
Add com.mysql.jdbc.Driver to CLASSPATH.
If you use maven, add to <dependencies> section of pom.xml the dependency of required driver (mysql-connector-java):
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
for gradle
compile group: 'mysql', name: 'mysql-connector-java', version: mysqlVersion
You need to add the downloaded JAR to the classpath.
Try this instruction:
Open NetBeans and right-click on the project name in the Projects tab.
Select Properties.
Select Libraries.
Click the Add Jar/Folder button.
Navigate to the directory where the downloaded JAR files are.
This question already has answers here:
How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception
(13 answers)
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 3 years ago.
I've been working on a web project with Java EE and PostgreSQL and it runs perfectly. I tried moving it to another machine, but now it's not connecting to the database anymore.
I tried multiple solutions but nothing works so far:
adding postgresql.jar to java build path
moving it to the /lib file of tomcat server
Hers is my connection to database class:
public class DBConnexion {
private static Connection con=null;
private DBConnexion(){
try
{
try {
Class.forName("org.postgresql.Driver");
//getConnection(url:dataBase name, owner name , password)
con=(Connection)DriverManager.getConnection("jdbc:postgresql:septentrion", "postgres","123");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
catch(SQLException e1)
{
e1.printStackTrace();
}
}
public static Connection getInstance()
{
if(con==null)
new DBConnexion();
return con;
}
}
Depending on your dependency manager, you will have to add postgresql as your dependency. For example if you are using Maven then
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.6</version>
</dependency>
If you are not using anything(which I think is the case), then you will have to manually download the jar file and put it in your classpath.
EDIT: Since you do not use any dependency manager,
if you want to compile com.example.Foo that depends on lib/postgresql-42.2.6.jar you might use the following incantation:
javac -classpath lib/postgresql-42.2.6.jar com/example/Foo.java
I am trying to open a MySQL connection but run into this error.
java.lang.ClassNotFoundException: com.mysql.jdbc.driver
There are plenty of questions online about this but I cannot seem to make it work.
I currently using gradle to compile 'com.oracle:ojdbc14:10.2.0.4.0' and I have added the connector library using the build in maven importer.
My code looks like this:
private static final String DRIVER = "com.mysql.jdbc.driver";
...
try {
try {
Class.forName(DRIVER).newInstance();
} catch (Exception e)
{
e.printStackTrace();
}
...
}
I tried to add ojdbc-14.jar to libs and reference that in gradle without success. I also use some groovy code in the build.settings but without success, I'm not sure how to work with that either.
A pic of my IDE:
The error is in your DRIVER string. It should be
DRIVER = "com.mysql.jdbc.Driver"
I've searched a lot and spent many time trying register JDBC driver.
First, I copied my ojdbc7.jar file (downloaded from Oracle) into directory shown below:
Driver File(s): /Users/Kamil/glassfish4/jdk7/jre/lib/ext/ojdbc7.jar
Driver Class: oracle.jdbc.OracleDriver
// this is copied from Services/Databases/Drivers/ojdbc
Then, I tried following code:
try {
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(myDriver);
} catch (ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
... and this one:
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
... and with this line instead:
Class.forName("oracle.jdbc.driver.OracleDriver");
I always get ClassNotFoundException :(
Here is code I try to run:
Connection DBconn;
String USER = "root";
String PASS = "root";
System.out.println("Connecting to database...");
DBconn = DriverManager.getConnection("mysql://localhost:3306/RestToolDatabase", USER, PASS);
System.out.println("Creating statement...");
Statement stmt = DBconn.createStatement();
String sql;
sql = "select surname, id, age\n"
+ "from customers \n"
+ "where name = \"maria\" \n"
+ "order by id;";
ResultSet rs = stmt.executeQuery(sql);
I've also read about setting the classpath as described here:
Right-click your Project.
Select Properties.
On the left-hand side click Libraries.
Under Compile tab - click Add Jar/Folder button.
but there is no "Properties/Libraries" option in NetBeans...
I use Maven and there is following dependency added by some library:
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>14</version>
<type>pom</type>
</dependency>
Maybe there is some workaround, or other way to add it automatically? It SHOULD be simple but I'm unexperienced and wasted many time on this. Please help.
EDIT: Thank you for replies, yes, I use MySQL Server at localhost:3306 [root]. I have MySQL JDBC connector installed here:
/Applications/NetBeans/NetBeans 8.0.app/Contents/Resources/NetBeans/ide/modules/ext/mysql-connector-java-5.1.23-bin.jar
When I go to "Services" --> "Drivers" --> "MySQL (Connector/J driver)" there is Driver Class path exactly as you suggested, so I use Class.forName("com.mysql.jdbc.Driver") now.
I right-clicked on "MySQL (Connector/J driver)" driver and went to "Connect Using..." --> "localhost, port 3306, user, password". And it is connected now, I see new connection. But still I get ClassNotFoundException.
EDIT 2 - this solution worked for me:
I added following to dependencies in pom.xml:
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
</dependency>
... and builded application; the driver was downloaded and installed. As simple as that... I spent many time on this... It works - yeah! :)
You are using an Oracle JDBC driver on a MySQL database, you should use
Class.forName("com.mysql.jdbc.Driver");
Edit: Thanks to the comments from #duffymo and #Mark-Rotteveel who noticed that the URL of the connection is also wrong, the correct connection is:
Connection DBconn;
String USER = "root";
String PASS = "root";
DBconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/RestToolDatabase?" + "user="+USER+"&password="+PASS);
Setting the classpath :
Download the JAR JDBC Connector/Driver
Copy file in subfolder of project (ex: libs) (not 'src')
Project->Properties->Libraries->Add Jar/Folder->Select file
And packaging dependent librairies with relative path in 'dist' :
Project->Build->Packaging->Check "Copy Dependent Librairies"
I don't think the JDBC JAR belongs in that directory. The CNF exception backs me up.
I would recommend putting that JAR in your project WEB-INF/lib, repackaging the WAR, and trying again.
This line is correct:
Class.forName("oracle.jdbc.driver.OracleDriver");
The full class name has to match that in the JAR. This is the only one that does.
You have to use the driver that matches your database: Oracle for Oracle, MySQL for MySQL. What database are you using?
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
This worked for me. Since there's no Property and Library in Netbeans apache. Good luck.