No suitable Driver error - java

public void connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:msql://23.249.225.135:3306/";
Connection conn = DriverManager.getConnection(url,"s****d","1*****-");
System.out.println("DB works");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
I have the JDBC driver in the /lib folder in my class path.
Keep getting:
No suitable driver found for jdbc:msql://23.249.225.135:3306/
Any ideas?

You've misspelled mysql in the url is msql.

The version of your mysql database doesn't support the version of your driver or vice-versa.
Look for the compatibility matrix here: Chapter 2 Connector/J Versions

Related

Connect Java to remote DB2 from neatbeands

I'm trying to do a small application that take some data from a db by connecting to a remote DB2 server using following example:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionExample
{
public static void main(String[] args) {
String jdbcClassName="com.ibm.db2.jcc.DB2Driver";
String url="jdbc:db2://localhost:50000/exampledb";
String user="db2inst1";
String password="password";
Connection connection = null;
try {
//Load class into memory
Class.forName(jdbcClassName);
//Establish connection
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(connection!=null){
System.out.println("Connected successfully.");
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
I get this error:
com.ibm.db2.jcc.am.SqlException: [jcc][10389][12245][3.67.27] Errore nel caricamento della libreria nativa db2jcct2, java.lang.UnsatisfiedLinkError: no db2jcct2 in java.library.path: ERRORCODE=-4472, SQLSTATE=null
further infromation here:
http://www.justexample.com/wp/connect-db2-java/
http://www-01.ibm.com/support/docview.wss?uid=swg21419978
I don't understand where to find missing library, on the JDBC library downloaded from the IBM site is missing, have I to copy it from the remote DB2 server or I have to point to the remote location?
thanks in advance best regards.
I found specific package inside IBM embedded software
I suppose you know how to add a jar file in the library of the app. The driver that you are looking for can be found in the IBM folder that generates when you install DB2.
For the driver go to C:/Program Files/IBM/SQLIB/Java there you can find the db2jcc.

Connection to a sql database questions

I'm trying to connect to my sql database on localhost.
With the following code :
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(ClassNotFoundException e) {
System.out.println("JdbcOdbcDriver wasn't found");
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/testBase", "root", "");
Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("");
}
catch(SQLException e){ System.out.println(e); }
finally {
if(connection != null) {
try { connection.close(); } catch (SQLException e) { e.printStackTrace(); }
}
}
I have 2 questions related to this code :
I am using the sun.jdbc.odbc.JdbcOdbcDriver driver because it seem that it was the only available on my system, however I've read that it's a windows component... Is there a way to make it platform independent (or is it already) ?
The second catch outputs a
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost/
Which is quite annoying and I don't seem to be able to find a way to work...
Thanks for your help !
When you generate jar, you can include all its dependencies.
So, you can use any driver you need and include it when you generate jar for the application.
Now you're cross platform.
About the question #2, that's because you don't have the mysqLconnector at lib folder.
You can download it here: http://dev.mysql.com/downloads/connector/j/
Just include it, if you're using IDE, you can look for a option "include jar/folder" and add it, then, try again; if you aren't in a IDE ... you have to set it on classpath.

Cant connect to specific schema in mysql

i'm trying to connect with jdbc to a specific schema in mysql server, the schema name is mining
when i'm trying to connect i get access to all the DB and therefore the executing statements apply to all the schemes in my db instead only to "mining"
this is how i establish a connection:
public class Mining {
Connection conn;
void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
this.conn = DriverManager.getConnection(
"jdbc:mysql://localhost/?currentSchema=mining","admin","admin" );
//I ALSO TRIED THIS: "jdbc:mysql://localhost/mining","admin","admin"
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
any thoughts?
thanks!
try,
private Connection connect = DriverManager
.getConnection("jdbc:mysql://localhost/mining?"
+ "user=admin&password=admin");
String databaseURL = jdbc:mysql://hostName:portNumber/schemaName?
Connection connectionObj = DriverManager.getConnection(databaseURL,userName,password);

Java: Connect to MS-Access Database (mdb or mde)

I am trying to connect to MS-Access using JDBC:ODBC:
public boolean connectToAccess(String accessFilePath) {
//Get connection to database
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
myConnection = DriverManager.getConnection("jdbc: odbc: driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessFilePath);
} catch (Exception ex) {
System.out.println(ex);
return false;
}
return true;
}
I get the error:
"No suitable driver found for jdbc: odbc: driver={Microsoft Access Driver (*.mdb)};DBQ=file.mdb"
Why?
Can you suggest another way of reading access files in Java?
Take those spaces out of the connection string and see if that helps. I'd also recommend printing the stack trace.
public boolean connectToAccess(String accessFilePath) {
//Get connection to database
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
myConnection = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessFilePath);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
The other way to read Access files is using the Jackcess library.
Try to create a DSN for the Access database from odbcad32. Another issue may be, the driver is not installed on your machine or you have insufficient privileges.

java.lang.ClassNotFoundException: com/microsoft/sqlserver/jdbc/SQLServerDriver

I am really new to JAVA but need to call a SQL Server function which I have been given access to.
I have built a JAVA call into a pl/sql function and am successfully calling it from one of my environments. When I move to another environment I get the error
ORA-29532: Java call terminated by uncaught Java exception: java.lang.ClassNotFoundException: com/microsoft/sqlserver/jdbc/SQLServerDriver
I have researched this to death and checked the correct installation of JAVA which seems fine but I'm obviously missing something. I need to somehow trace what is different on this environment, the fact that it runs in the other envionmnet proves that the class is correct so it has to be a config issue.
JAVA Class
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.net.Socket;
import java.io.IOException;
public class xxiceHJ
{
protected static String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
protected static String DB_URL = "jdbc:sqlserver://999.999.99.99:1433";
protected static String USER = "xxxx";
protected static String PWD = "xxxxx123$";
public static String getOrderStatus (String OrderNumber) throws SQLException, Exception
{
Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
String Message = null;
String WarehouseId = "01";
try
{
// Register JDBC driver
Class.forName(JDBC_DRIVER);
//Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PWD);
cs = conn.prepareCall("{call usp_get_order_status(?,?,?)}");
cs.setString(1, WarehouseId);
cs.setString(2, OrderNumber);
cs.setString(3, Message);
rs = cs.executeQuery();
//if prodeure return a value
if (rs.next())
{
Message = rs.getString(1);
}
//Clean-up environment
rs.close();
cs.close();
conn.close();
// }
// catch (SQLException se)
// {
// //Handle errors for JDBC
// cfFileNumber = "SQLException" + se.toString();
// se.printStackTrace();
// }
// catch (Exception e)
// {
// //Handle errors for Class.forName
// cfFileNumber = "Exception" + e.toString();
// e.printStackTrace();
}
finally
{
//finally block used to close resources
try
{
if (rs!=null)
{
rs.close();
}
}
catch (SQLException se2)
{
//nothing we can do
}
try
{
if (cs!=null)
{
cs.close();
}
}
catch (SQLException se2)
{
//nothing we can do
}
try
{
if (conn!=null)
{
conn.close();
}
}
catch (SQLException se)
{
se.printStackTrace();
}
}
return Message;
}
}
The CLASSPATH variable is the search string that Java Virtual Machine (JVM) uses to locate the JDBC drivers on your computer. If the drivers are not listed in your CLASSPATH variable, you receive the following error message when you try to load the driver:
java.lang.ClassNotFoundException: com/microsoft/jdbc/sqlserver/SQLServerDriver
The JDBC driver is not part of the Java SDK. If you want to use it, you must set the classpath to include the sqljdbc.jar file or the sqljdbc4.jar file. If the classpath is missing an entry for sqljdbc.jar or sqljdbc4.jar, your application will throw the common "Class not found" exception.
The sqljdbc.jar file and sqljdbc4.jar file are installed in the following location:
\sqljdbc_\\sqljdbc.jar
\sqljdbc_\\sqljdbc4.jar
The following is an example of the CLASSPATH statement that is used for a Windows application:
CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc.jar
The following is an example of the CLASSPATH statement that is used for a Unix/Linux application:
CLASSPATH =.:/home/usr1/mssqlserverjdbc/Driver/sqljdbc_4.0/enu/sqljdbc.jar
You must make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server, such as either sqljdbc.jar or sqljdbc4.jar.
For more information, please see:
http://support.microsoft.com/kb/313100
http://msdn.microsoft.com/en-us/library/ms378526.aspx
first Please download correct sql driver and then check your are using correct connection driver as per operating system. then once you have to test your connection if its working fine then you will go to next .
so please check this url
microsift sql server driver for linux
https://msdn.microsoft.com/en-us/library/hh568451(v=sql.110).aspx
my sql server driver for linux
https://dev.mysql.com/downloads/connector/j/5.0.html

Categories