cannot connect to mssql server from java jdbc - java

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.

Related

Java connect to Amazon Redshift

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.

ClassNotFoundException : com.mysql.jdbc.Driver but it's in the classpath

enter image description here
I have this Exception but the Jar file are in the referenced libraries.
I dont't know where is the problem. The code is alright and I have added all in the build path.
String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
final String DBMS = "jdbc:mysql";
final String SERVER="localhost";
final String DATABASE = "mapDB";
final int PORT=3306;
final String USER_ID = "MapUser";
final String PASSWORD = "map";
Connection conn;//gestisce una connessione
private void initConnection() throws DatabaseConnectionException {
try {
Class.forName(DRIVER_CLASS_NAME);/
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn=(Connection)DriverManager.getConnection(DBMS + "://" + SERVER + ":" + PORT + "/" + DATABASE,USER_ID,PASSWORD);
}catch(SQLException ex) {
throw new DatabaseConnectionException();
}
}
Your image shows you have mysql-connector-java-5.1.7-bin.jar on your classpath.
When looking for that file in the Maven Repository, there are many 5.1.x versions, but not 5.1.7.
It would seem that 5.1.7 is flawed and have been retracted.
Try using another 5.1.x version, e.g. the latest, which currently is 5.1.46.
It appears that there are different versions of MySQL JDBC connectors, as the second print shows.
Maybe the existence of 2 references to the same lib is causing the ClassNotFoundException, or your project's build is not up to date.
I suggest you to keep only one version of the connector on the references of your project, clear and build the project again.
Have you tried removing mysql-connector-java-5.1.7-bin.jar, just keep mysql-connector-java-8.0.11.jar

Trying to connect to my sql database getting exception saying "no suitable driver found"

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.

Java program and mySQL connectivity issue: No suitable driver found

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

Java Connect to database Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Hey stupid question but I'm having a hard time connecting my java program to a mysql database.
Throwing an exception when I hit this line.
Class.forName(driverName).newInstance();
The driver name is com.mysql.jdbc.Driver. I've searched around a bit on google and found something about a mysql-connector.jar file that I'm apparently supposed to have but I really haven't looked into it yet. Thanks.
Entire code:
Connection connection = null;
try
{
String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
Class.forName(driverName).newInstance();
String serverName = "*********";
String database = "canteen_web3";
String url = "jdbc:mysql://" + serverName + "/" + database;
final String username = "*****";
final String password = "******";
connection = DriverManager.getConnection(url,username,password);
System.out.println("Connected!");
}
catch(Exception ex)
{
throw new ICException(ex.getMessage());
}
Start your app with
java -classpath .:mysql-connector.jar MyClass
The colon separates two paths. The . is the directory you are in (and hopefully the class or the base package), the latter jar is the driver.
For further information refer to the various sources of documentation http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html

Categories

Resources