I am creating a java console application that connects to a MySQL database. I have outlined below what I've done but the connection is not being established:
Things I have done:
1) I have added the mysql-connector-java-5.1.36.jar to my project bulid path.
2) I have written the code that tries to establish a connection
Can anyone suggest what I'm doing wrong?
The code is given below:
import java.sql.Connection;
import java.sql.DriverManager;
public class main {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:1234/first","root","");
System.out.println("Success fully Connected");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Related
I have a problem connecting to SQL-server database through from my android project. I have added sqljdbc41.jar file to my /app/libs directory and I have added it to dependencies in my android studio project.
I use following code:
package com.konrad.rezerwacje1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Database_Console {
public static void openConnection(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";
Connection con = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
openConnection();
}
}
yet i still get this error
java.sql.SQLException: No suitable driver found for jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
Instead of this :
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";
You have to use this :
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=my_db";
Connection con = DriverManager.getConnection(url, "username", "password");
Note the different classname, and the fact that prefix jbdc in the URL has been changed to jdbc.
If it is not a requirement to go with sqljdbc41.jar, then you might consider using the jtds driver for your requirement to connect to SQL Server 2014 with Android Studio. There are tons of articles that can help you start with this set of technologies.
For a primer, here are the details:
Download the JTDS driver from here
Then import this jar into your Android Studio, eg: jtds-1.2.5.jar
Use the following details in your code:
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/DATABASE;user=sa;password=p#ssw0rd");
I have a Java application and i need it to connect with my MySQL database's SQL script using JDBC.
Here is my Java application:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package basic;
import basic.ScriptRunner;
import javax.swing.*;
import java.sql.*;
import java.io.*;
/**
*
* #author User
*/
public class javaconnect {
Connection conn = null;
public static Connection ConnectDb(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/new","login","pass");
/*As we are creating a connection on a local computer we will write the url as jdbc:mysql://localhost:3306 */
ScriptRunner runner = new ScriptRunner(conn, false, false);
runner.runScript(new BufferedReader(new FileReader("D://Java Lenti/EkonomiSoftware/src/basic/new.sql")));
return conn ;
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;}
}
}
The problem is that the Java application connects through MySQL Server, not through the SQL Script. I think the problem is at Connection parameters I gave. Can anyone guide me how to change the connection to make it connect to the SQL script not to the server?
It worked for me:
package com.spring.sample.controller;
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager
.getConnection(
"",
"", "");
ScriptRunner runner = new ScriptRunner(conn, false, false);
runner.runScript(new BufferedReader(new FileReader("new.sql")));
} catch (Exception e) {
e.printStackTrace();
}
}
}
new.sql
insert into hello(name) values ('test');
May be there're some mistakes at your sql file.
I have created a simple Java connection script in Java to connect to a database shown below.
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class dbconn {
public static void main(String[] args) throws ClassNotFoundException
{
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:C:/Program Files (x86)/Spiceworks/db/spiceworks_prod.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
}
}
Now I have tested this connection via some SQL queries inside the dbconn script and it works.
However my question is, how would Icall this instance of the database into another form in the same project to preform the same SQL queries which are:
ResultSet resultSet = null;
resultSet = statement.executeQuery("SELECT * FROM users");
while (resultSet.next())
{
System.out.println("EMPLOYEE Email: " + resultSet.getString("email"));
}
You can retain and reuse the Connection, saving it probably in some static field. If you access it from multiple threads, SQLite must work in the Serialized mode. You must have the code somewhere to re-establish the connection if it has been lost for some reason.
If the use of Connection is not heavy, you can also have some method that opens it and close when no longer needed, better inside the finally block.
this is my code snippet,
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class Delete
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("mysql:jdbc://localhost:3306/raja","root","459805");
Statement stmt=con.createStatement();
int count=stmt.executeUpdate("DELETE GENNU WHERE USER_ID=3;");
if(count>0)
System.out.println(" Ok Deletion done");
}
catch(ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
}
and when I execute it , i got like this.
actually you have an error in your DELETE statement, you lack FROM keyword. it should be
DELETE FROM GENNU WHERE USER_ID=3
see the error, it's pointing on DELETE.
UPDATE 1
try, jdbc:mysql not mysql:jdbc
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/raja"
+ "user=root&password=459805");
MySQL and JAVA JDBC
Try with
Class.forName("com.mysql.jdbc.Driver").newInstance();
The documentation says:
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
Also, the URL should be
jdbc:mysql://localhost/3306/raja
and not
mysql:jdbc://localhost/3306/raja
You need the mySQL Java Connector. Which you can find at the download page here:
https://www.mysql.com/products/connector/
I am trying to do a simple connection test to a postgresql database, the code is working well but I am getting the following message:
PostgreSQL 9.0 JDBC4 (build 802)
Found in: jar:file:/C:/thales/Dropbox/study/java/jars/postgresql-9.0-802.jdbc4.jar!/org/postgresql/Driver.class
I am using the jdbc file provided by the address http://jdbc.postgresql.org/download.html
Following, the code:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class connector {
public static void main(String[] argv) {
System.out.println("Checking if Driver is registered with DriverManager.");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
System.out.println("Couldn't find the driver!");
System.out.println("Let's print a stack trace, and exit.");
cnfe.printStackTrace();
System.exit(1);
}
System.out.println("Registered the driver ok, so let's make a connection.");
Connection c = null;
try {
// The second and third arguments are the username and password,
// respectively. They should be whatever is necessary to connect
// to the database.
c = DriverManager.getConnection("jdbc:postgresql://localserver/test","ping", "pong");
} catch (SQLException se) {
System.out.println("Couldn't connect: print out a stack trace and exit.");
se.printStackTrace();
System.exit(1);
}
if (c != null)
System.out.println("Hooray! We connected to the database!");
else
System.out.println("We should never get here.");
}
}
thanks for all feedback but i manage to solve the problem reinstallign eclipse (?? )
using the same code, and the same jdbc driver, the code described in the question worked.