Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Java MySQL connection

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
dutchgold92
Posts: 21
Joined: 2011-02-08 10:24

Java MySQL connection

#1 Post by dutchgold92 »

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.

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

Re: Java MySQL connection

#2 Post by emanuelmm »

have you set the port ???

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

dutchgold92
Posts: 21
Joined: 2011-02-08 10:24

Re: Java MySQL connection

#3 Post by dutchgold92 »

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.

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

Re: Java MySQL connection

#4 Post by emanuelmm »

Show

Pingu4ever
Posts: 21
Joined: 2015-06-19 15:19

Re: Java MySQL connection

#5 Post by Pingu4ever »

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 .

Post Reply