JDBC problems connectiong to sql server [duplicate] - java

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");

Related

Driver not found mysql java [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
How do I resolve ClassNotFoundException?
(28 answers)
Closed 4 years ago.
i have an problem with my java program i'm trying to connect to an MYSQL database but it says driver not found i've imported mysql-connector-java into the project even with output set so it exports with the program
the class:
package com.CloudyProductions.GCDSS;
import java.sql.*;
public class mysql {
public static Connection c;
static String host = "localhost";
static String port = "3306";
static String database = "";
static String username = "root";
static String password = "";
public static void connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
I've added the mysql-connector to the project via maven and did what you said but now get this error:
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
From this answer:
You will have to include driver jar for MySQL MySQL Connector Jar in your classpath.
If you are using command line include the path to the driver jar using the -cp parameter of java.
For example:
java -cp C:\path\to\connector.jar Main
You need to set your mysql driver jar into the classpath through command line as follows.
//for windows
set CLASSPATH=PATH_TO_JAR
//for unix
export CLASSPATH=PATH_TO_JAR
Or you can add it during execution of your application directly using -cp as,
java -cp PATH_TO_JAR your_class_app
or -classpath
java -classpath PATH_TO_JAR your_class_app

jdbc wont connect with java [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 4 years ago.
I downloaded the latest version of mysql connector "mysql-connector-java-8.0.11"
and I tried to connect it with java using netbeans but it won't work
also, I added the jar file in my project lib but nothing happened
it gives me this error:
Exception in thread "main" java.lang.ClassNotFoundException:
com.mysql.jdbc.cj.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at learnjdbc.Learnjdbc.main(Learnjdbc.java:18)
this is my code
import java.sql.*;
public class Learnjdbc {
public static String name="root";
public static String password="ismail19972018";
public static String url="jdbc:mysql://localhost/myinfo";
public static void main(String[] args) throws ClassNotFoundException {
Connection connect=null;
Statement stm=null;
PreparedStatement prstm=null;
ResultSet rs=null;
try{
Class.forName("com.mysql.jdbc.cj.Driver");
connect=DriverManager.getConnection(url,name,password);
System.out.println("connected");
}catch(SQLException ex){
ex.printStackTrace();
}
}
The name of the class that implements java.sql.Driver in MySQL Connector/J is com.mysql.cj.jdbc.Driver. The class name in the code com.mysql.jdbc.cj.Driver does not exists which causes the ClassNotFoundException.
cannot connect
This is not 'cannot connect', it is 'cannot find class', and the Class.forName() line has been unnecessary since 2007.
Just remove it.

ERROR:: No suitable driver found for jdbc:.. localhost 1572//project [duplicate]

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

JDBC connection MSQL error "No suitable driver found" [duplicate]

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.

java.lang.NoClassDefFoundError: com/healthmarketscience/jackcess/util/ErrorHandler in ms access connectivity with java8

I am trying to connect Ms Access database with java 8 version. But as in this version jdbcodbcbridge driver has been removed, so following jar files need to be included :
**ucanaccess-x.x.x.jar
HSQLDB (hsqldb.jar, version 2.2.5 or newer)
,Jackcess (jackcess-2.x.x.jar)
,commons-lang (commons-lang-2.6.jar, or newer 2.x version)
,commons-logging (commons-logging-1.1.1.jar, or newer 1.x version)**
I have bought all these jar files in my eclipse through Build Path option.
But still when i am executing the following code it is coming up with error as:
Exception in thread "main" java.lang.NoClassDefFoundError:
com/healthmarketscience/jackcess/util/ErrorHandler at java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Unknown Source) at
demo.JDBCDemo.main(JDBCDemo.java:11) Caused by:
java.lang.ClassNotFoundException:
com.healthmarketscience.jackcess.util.ErrorHandler at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) ... 3 more
And my code is:
import java.sql.*;
import java.util.*;
import com.healthmarketscience.jackcess.util.ErrorHandler;
public class JDBCDemo {
public static void main(String args[]) throws Exception
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection con=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\isha\\Desktop\\StudentData.accdb");
Statement stmt=con.createStatement();
String str="insert into NameData values(4,'ram')";
stmt.executeUpdate(str);
String s="select * from NameData";
ResultSet res=stmt.executeQuery(s);
while(res.next()){
System.out.println(res.getString(1)+":"+res.getString(2));
Enumeration e=DriverManager.getDrivers();
while(e.hasMoreElements()){
Driver d=(Driver)e.nextElement();
System.out.println(d.getClass().getName());
}
}
}
}
You have an old obsolete version of jackcess in your classpath. Please add to your classpath ucanaccess.jar the jars in the folder lib of the specific ucanaccess distribution you're using.
String str="insert into NameData values(4,'ram')";
in which columns you insert this values?

Categories