Java MySQL connection

Need help with C, C++, perl, python, etc?

Java MySQL connection

Postby dutchgold92 » 2011-04-17 19:01

Having trouble connecting to MySQL on my Squeeze server using a Java application, to illustrate it's something like this:
Code: Select all
import java.sql.*;

class Connect
{
   public static void main(String args[])
   {
      Connection conn = null;
           try
           {
               String userName = "username";
               String password = "password";
               String url = "jdbc:mysql://host/database";
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
           }
           catch (Exception e)
           {
               System.out.println ("Cannot connect to database server");
           }
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
   }
}


It compiles but cannot connect (returns the exception) locally or remotely. The same username/password combination works fine in a PHP script.
I'm not sure if the problem is related to my program or my server, if anyone can offer suggestions. I installed libmysql-java, incase that made any difference.
dutchgold92
 
Posts: 21
Joined: 2011-02-08 10:24

Re: Java MySQL connection

Postby emanuelmm » 2011-04-29 11:43

have you set the port ???

like this :
jdbc:mysql://localhost:3306/database
emanuelmm
 
Posts: 2
Joined: 2011-04-19 11:08

Re: Java MySQL connection

Postby dutchgold92 » 2011-04-29 12:02

emanuelmm wrote:have you set the port ???

like this :
jdbc:mysql://localhost:3306/database


Yes, cheers for the reply. Turned out I just had to set MySQL to listen on 0.0.0.0.
dutchgold92
 
Posts: 21
Joined: 2011-02-08 10:24

Re: Java MySQL connection

Postby emanuelmm » 2011-04-29 17:45

Show
emanuelmm
 
Posts: 2
Joined: 2011-04-19 11:08

Re: Java MySQL connection

Postby Pingu4ever » 2017-01-17 10:28

Hi to all,

I also have installed
Code: Select all
MySQL
on my debian stretch machine. I tried to use the connector/J from MySQL by installing
Code: Select all
libmysql-java
, but Java seems to ignore the newly installed connector. If I for example run

Code: Select all
import java.sql.*;

public class MYDemo {
  public static void main(String[] args) throws SQLException, ClassNotFoundException {
    // Load the JDBC driver
    Class.forName("org.mysql.jdbc.Driver");
    System.out.println("Driver loaded");

    // Try to connect
    Connection connection = DriverManager.getConnection
      ("jdbc:mysql://localhost:3306/Database", "user", "passw");

    System.out.println("It works!");

    connection.close();
  }
}



i get:

Code: Select all
Exception in thread "main" java.lang.ClassNotFoundException: org.mysql.jdbc.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:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at MYDemo.main(MYDemo.java:6)


It seems to me that on the Debian side there is no documentation as to what is to be done or additionally to set up to successfully run that program. On the net you will find lots of topics around the above issue but whatever is suggested there seems not to be clean and the method to use within the Debian setting. On the MySQL homepage there you have suggestions to put links and set path variables etc. but in order exactly to know what to do here it seems to me that it would be helpful to know what links the above driver sets and what the correct paths are. What is the correct way to set up the above environment and f.e. later a maven environment? Thanks for any answer. And ... all hail to Debian ;)

Edit: Progress/ Problem solved
Since the JDBC driver is a Type 4 JDBC driver, this means that we don't have to use the Class.forName(...) command. If you comment that out, the program works (at least then when you have set the CLASSPATH (in my case) as
Code: Select all
export CLASSPATH=/usr/share/java/mysql-connector-java-5.1.40.jar:.

in f.e. your .bashrc .
Pingu4ever
 
Posts: 21
Joined: 2015-06-19 15:19


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable