0

I need to setup a Java J2SE application in Windows 7, so that it can connect to a MySQL database. I did this without problem back in my university days, but that was well over 10 years ago. Up until now, I've not needed to do it since then and at this point I just can't get it working.

I already have MySQL & MySQL Workbench installed and working on the machine where I'm trying to write the J2SE application. I've also setup a database in MySQL on that machine that already has data in it.

After doing all that I installed NetBeans in the same machine and also wrote a simple Java desktop application that I aim to continue developing until I get the job finished. At this stage that application is just an empty JFrame, but I can still write database connection code in it to try and get my connection working.

The code below is simplified, but does still show all the MySQL connection code of what I've tried so far. More or less all of the connection code is code that I copied from MySQL web pages that show how to do what I'm trying to do:

{
import java.awt.*;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class Database
{
    public static void main(String[] args) { new Database(); }

    JFrame frame;
    Statement stmt = null;
    ResultSet rs = null;

    public Database()
    {
        try
        {
            // I think this is where my problem is because when I try 
            // running the program, I just get an error message saying:
            // 'No suitable driver found for jdbc:mysql.... etc'
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
        catch (Exception ex)
        {
            // handle the error
        }


        Connection connection = null;

        try
        {
            // my connection details and query are all 
            // correct, but I've changed them here.
            connection = DriverManager.getConnection("jdbc:mysql://localhost/database?" +
            "user=root&password=password");

            stmt = connection.createStatement();
            rs = stmt.executeQuery("SELECT * FROM table_name");

            if (stmt.execute("SELECT * FROM table_name"))
            rs = stmt.getResultSet();
        }
        catch (SQLException ex)
        {
            // handle any errors - top one seems to be giving 
            // me the 'No suitable driver found' message.
            System.out.println("SQLException: " + ex.getMessage()); 
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }

        // the rest of this just sets up and displays my empty JFrame.
        // I don't need any help with this part.
        frame = new JFrame();
        frame.setTitle("Database");
        frame.setResizable(false);

        Dimension dim = frame.getToolkit().getScreenSize();

        int width = 1290;
        int height = 980;
        frame.setSize(width, height);
        frame.setLocation((dim.width - width)/2, (dim.height - height)/2);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
    }
}

As stated above I get the 'No suitable driver found for jdbc:mysql' message in the NetBeans output window when I try running this code.

I have found online how I need to set the environmental variables for JDBC/MySQL and as a result I've set them as follows:

JAVA_HOME C:\Program Files (x86)\Java\jdk1.7.0_55

CLASSPATH C:\Program Files (x86)\Java\jdk1.7.0_55\jre\lib

Path C:\Program Files\Java\jre1.8.0_60\bin

Each var is obviously inserted into two separate fields in the environmental variables window/panel (these are 'variable name' and 'variable value'). However, they both come up on the same line, even when I try inserting them on two separate lines in this 'stack overflow' page.

The variables have all been set as System variable (not user variables). Also, the paths are all valid in that there is a real Java folder on my machine at each location (with matching folder names) and the paths are also paths that I found listed online. Also, the Path variable has been added to the end of an existing string contianing other Path vars. With that var I put an ';' on the end of the existing string before adding the new var (as listed above) after the ';'.

From what I remember of doing this all those years ago, I think I did previously either set the driver file up somewhere where it could be found by my code, or use some sort of Java class/function to point the app to where the driver was (it could even have been a combinaion of both of those things?). However, I really can't remember exactly what was done in that regard and I can't currently find anything that shows me how to do it online.

I have also tried following the NetBeans instructions for setting up the database connection. I did most of that without any problem, but found something that I couldn't make sense of in the instructions. I'm told that I need to select something for 'Path/URL to admin tool' in the MySQL Server Admin Properties along with something else in an 'Arguments' field that's directly below 'Path/URL to admin tool'. The instructions seem to be telling me to do something that doesn't seem possible, because I can't find the location that they are asking me to select as the 'Path/URL' and I also can't make sense of what I'm being asked to enter in the arguments field.

Anyway that does more or less cover everything that I've tried so far. I hope that there's a straightforward way of resolving this problem. Big thanks in advance to anyone who offers any help.

craig-c
  • 161
  • 2
  • 5
  • 1
    Make sure that you've included the JDBC driver for MySQL in your project. Right click the "Libraries" node of your project and select "Add JAR/Folder" option and browse to the folder where the JDBC driver Jar is located and select it (not the folder) – MadProgrammer Sep 28 '15 at 01:01
  • Yep, that seems to have solved the problem, so big thanks for that one. (I no longer see the 'No suitable driver found for jdbc:mysql' message anyway). – craig-c Sep 28 '15 at 01:10
  • Onwards to the next problem ;) – MadProgrammer Sep 28 '15 at 01:11
  • I did search online thoroughly for a solution to my problem before posting my question. I did find a number of pages that discussed the same sort of problem, including one or two stack overflow pages, but none of those actually helped me resolve my problem. That's why I felt I had to post my question. – craig-c Sep 29 '15 at 14:49
  • Also the response I got from 'MadProgrammer' soon after posting my question did actully result in me resolving the problem, so my problem has now been solved. Therefore, although you've said that the problem had been resolved preoviously, I either didn't find the post you were talking about, or that post didn't actually give answers that were relevent to my particular problem. – craig-c Sep 29 '15 at 14:54

0 Answers0