I've been having a problem with mySQL database connectivity. I'm getting an error:
No suitable driver found for jdbc:mysql://127.0.0.1/sakila.
I have installed mySQL workbench, and have the driver from here
http://dev.mysql.com/downloads/connector/j/
I have saved mysql-connector-java-5.1.18-bin and set the classpath to
C:\Program Files\Java\jre7\lib\mysql-connector-java-5.1.18-bin;
and started the mysql workbench where the database is found.
The code I am using is as follows: Which I am sure works, as I've asked a friend to test it form me. Unfortunately, we are developing on different platforms and could not instruct me as to how to fix this error. Has anyone an idea on how I can fix this?
public class Version {
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://127.0.0.1/sakila";
//String url = "jdbc:mysql://localhost:3306/sakila";
String user = "root";
String password = "root";
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("select * from actor;");
System.out.println("test");
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
EDIT: Problem sovled. Did not have .jar appended to the end of the bin file, which is necessary.
You need to instantiate the driver before calling the getConnection :
String pdriver = "com.mysql.jdbc.Driver";
Class.forName(pdriver).newInstance();
Add the following
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
right before the line "con = DriverManager.getConnection(url, user, password);"
All you need to do is load the driver class before getting the connection from the drivermanager.
You need to place the connector jar file to your classpath or ...\jre1.6.0\lib\ext
Classpath is the one you should favor instead of the latter
You need to add the MySQL connecter library jar file to the classpath, rather than the directory where it is contained.
Are you not using an IDE like Netbeans or Eclipse? Setting up a command line development environment in Windows is not hard but it's not trivial either
Related
I am trying to write code for bringing a text file's data into a database using Eclipse, MySQL Workbench, and JDBC 8.0.11. It is giving me a ClassNotFoundException. I have looked at multiple other questions, and they have all been fixed by putting the java\com\mysql\jdbc\Driver.java inside the DriverManager.getConnection parameter. I have already done that, and it is still giving me an error. Anyone have any ideas as to why I'm still getting this error?
public static void main(String[] args) throws Exception{
Class.forName //Register JDBC Driver
("*mysql-connector-java-8.0.11.\\src\\legacy\\java\\com\\mysql\\jdbc\\Driver.java*")
.newInstance();
conn = DriverManager.getConnection (url, user, pass);
Statement stmt = conn.createStatement();
String mysql1 = "UPDATE Policy SET " + readAndArray //Changeable file path
("filepath");
}
NEW EDIT
Following #zlakad 's advice, it turns out that you don't need to use Class.forName() if you have Java 6 or higher. Although, now I have a new error: SQLNonTransientConnectionException because of the underlying WrongArgumentException. I'm puzzled as to why it does this because I'm not using the incorrect parameters for DriverManager.getConnection. Any suggestions?
String url = "file path"; //Changeable for MySQL DB
String user = "root";
String pass = "password";
public static void getConnection() throws Exception {
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
Try this:
// None of this belongs in a main method.
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
// where are url, user, pass set? I don't see them.
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
// this is simply wrong.
String mysql1 = "UPDATE Policy SET " + readAndArray("filepath");
}
You're new to Java and JDBC. This is not a good way to do it. I'd recommend that you search the web and SO for some examples of how to do it better.
You have to load driver class for connection not jar file of that class
you shoud try this:
Class.forName("com.mysql.jdbc.Driver");
I was using the wrong format for a database url in the DriverManager.getConnection();
I changed my url to a jdbc:mysql://host:3306/ and it worked.
String url = "jdbc:mysql://*host*:3306/";
Connection conn = DriverManager.getConnection(url, user, pass);
I am having trying to connect to Amazon Redshift Database with my Java code. I found a code snippet on AWS website that should work. However I am running into problems with implementing the JDBC driver.
This is the website and the code from the website: https://docs.aws.amazon.com/redshift/latest/mgmt/connecting-in-code.html
package connection;
import java.sql.*;
import java.util.Properties;
public class Docs {
//Redshift driver: "jdbc:redshift://x.y.us-west-
2.redshift.amazonaws.com:5439/dev";
//or "jdbc:postgresql://x.y.us-west-2.redshift.amazonaws.com:5439/dev";
static final String dbURL = "***jdbc cluster connection string ****";
static final String MasterUsername = "***master user name***";
static final String MasterUserPassword = "***master user password***";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//Dynamically load driver at runtime.
//Redshift JDBC 4.1 driver: com.amazon.redshift.jdbc41.Driver
//Redshift JDBC 4 driver: com.amazon.redshift.jdbc4.Driver
Class.forName("com.amazon.redshift.jdbc.Driver");
//Open a connection and define properties.
System.out.println("Connecting to database...");
Properties props = new Properties();
//Uncomment the following line if using a keystore.
//props.setProperty("ssl", "true");
props.setProperty("user", MasterUsername);
props.setProperty("password", MasterUserPassword);
conn = DriverManager.getConnection(dbURL, props);
//Try a simple query.
System.out.println("Listing system tables...");
stmt = conn.createStatement();
String sql;
sql = "select * from information_schema.tables;";
ResultSet rs = stmt.executeQuery(sql);
//Get the data from the result set.
while(rs.next()){
//Retrieve two columns.
String catalog = rs.getString("table_catalog");
String name = rs.getString("table_name");
//Display values.
System.out.print("Catalog: " + catalog);
System.out.println(", Name: " + name);
}
rs.close();
stmt.close();
conn.close();
}catch(Exception ex){
//For convenience, handle all errors here.
ex.printStackTrace();
}finally{
//Finally block to close resources.
try{
if(stmt!=null)
stmt.close();
}catch(Exception ex){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
System.out.println("Finished connectivity test.");
}
}
I got my connection credentials, but I get a following error.
java.lang.ClassNotFoundException: com.amazon.redshift.jdbc4.Driver
which is caused by this line:
Class.forName("com.amazon.redshift.jdbc.Driver");
I don't have this driver implemented anywhere so the error makes sense. The problem is that IntelliJ IDEA has a plugin for this (Database Navigator) that doesn't work as expected and I couldn't get any help on their forums.
Is there any other to include the JDBC driver so the code can work with it (in IntelliJ)?
EDIT:
After adding the JAR as an external library, my project looks like this:
However, when I run the code, I get the same error.
You can import the Amazon Redshift JDBC driver like this :
Click on File
Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
click on modules on your left Modules
click on Dependencies
'+' → JARs
Dowload the Amazon Redshift JDBC driver from below URL.
https://docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection.html#download-jdbc-driver
Add the downloaded Jar to your classpath.
It will work.
You could follow steps as suggested by #Corentin to perform step #2.
I'm using the tutorial at http://www.vogella.com/tutorials/MySQLJava/article.html
to try tp connect to my sql server on my server
When it executes the line:
Connection connect = DriverManager
.getConnection("jdbc:mysql:http://www.findmeontheweb.biz"
+ "user=findmeon_bitcoin&password=PASSWORD");
an exception gets thrown saying "No sutabled driver found for jdbc:mysql:http://www.findmeontheweb.biz
This is what I did
1. Downloaded the "mysql-connecter-java-5.1.33.bin.jar into my lib folder
2. added the jar to my project from preferences.
project code:
public class cStart {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public static void main (String[] args) {
int g=0;
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
// EXCEPTION GOES OF HEAR
Connection connect = DriverManager
.getConnection("jdbc:mysql:http://www.findmeontheweb.biz"
+ "user=findmeon_bitcoin&password=PASSWORD");
} catch (Exception e) {
System.out.println("Exception...." );
}
}
}
The URL format should be look like this
jdbc:mysql://hostname/ databaseName
I think this is a much cleaner way to do it:
String URL = "jdbc:URL_TO_YOUR_DATBASE";
String USER = "username";
String PASS = "password"
Connection conn = DriverManager.getConnection(URL, USER, PASS);
As seen here: http://www.tutorialspoint.com/jdbc/jdbc-db-connections.htm
I say give that link a try with your driver. You also should make sure you have the actual jar for MySQL. It really might be invalid.
I would try the one here: http://www.java2s.com/Code/Jar/c/Downloadcommysqljdbc515jar.htm
And then add that to your project.
The URL to the database might be wrong.
If yes you should specify a correct one with including database name.
Else verify if the jdbc driver jar is added in the build-path, if yes try to put it in the lib folder of your webapp.
I am trying to connect to mssql server from java but I couldnt figure it out. It throws an exception
ERROR: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MLS_J
here is the code below. What am I doing wrong? How can I fix this?
public static String connection_test(){
String address = "jdbc:sqlserver://192.168.1.101:1433;DatabaseName=MLS_J";
String user = "sa";
String password = "xxxx";
try {
Connection conn = DriverManager.getConnection(address, user, password);
java.sql.Statement stmt = conn.createStatement();
return "Bağlantı kapalımı? - " + conn.isClosed();
} catch (Exception e) {
return "ERROR: " + e.getMessage();
}
}
You might wanna take a look at this.
Try adding this line to your code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Follow the steps for adding jar files in build path
Right click on project
click build path-> configure build path
click libraries folder
then click add external jar and give the path of the sqljdbc4.jar.
I would suggest you to use jtds driver.
package javaapplication1;
import java.sql.*;
public class JavaApplication1{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/TEST/ANKUR1";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection("TEST1/ANKUR","root","school");
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
System.out.println(e);
}
}
}
I had installed MySQL 5.5 on my machine and I copied Java-connector.jar file in the required folders. My Database is test1 and table is ankur. Please see I was using this code to just to check the connectivity with my NetBeans 7. Any suggestions?
The URL you actually use in getConnection(...) is wrong. You are not supposed to put in just the name of the table you want to work with. You need to have a complete URL so the JDBC system knows that it should use the mysql driver and where the mysql driver needs to connect to.
Recheck the example you copied from, and see where they use the "url" variable.
The JDBC URL format when using the MySQL Connector/J driver is:
jdbc:mysql://[host][,failoverhost...][:port]/[database][?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
Therefore, the actual URL to be used in your case ought to be (if the database is installed on your local machine with name TEST and listening on the default MySQL port of 3306):
jdbc:mysql://localhost:3306/TEST
and not the below.
TEST/ANKUR1
Remember that JDBC URLs are used by the DriverManager class to determine which driver ought to be used to connect to the database, and where the database is located. One does not connect to tables in JDBC; instead, one connects to a database (or a schema) and then issues SELECT queries against tables. Your method therefore, would look like:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/TEST","root","school");
System.out.println("Connected to the database");
PreparedStatement pStmt = conn.prepareStatement("SELECT * FROM ANKUR");
ResultSet rs = pStmt.executeQuery();
...
//Process the contents of the ResultSet
...
rs.close();
pStmt.close();
conn.close();
It should be noted that it a good practice to close the ResultSet, PreparedStatement and Connection objects in a finally block.