Skip to main content
gamedev.net
🔒 Locked

[java] SQL connection with executable jar

Started by chris_j_pook 3 replies 1k views
Original Post
chris_j_pook
chris_j_pook
Hi, I have the following code wich works fine for connecting to my sql database.

import java.sql.*;
import javax.swing.*;
import java.util.*;

public class MySqlConnector
{
    public MySqlConnector(String user,String password, ArrayList theTeams)
    {
        try
        {
            String url="jdbc:mysql://localhost/tournament?" +
                       "user=usr101&password=pass";

            String driver="org.gjt.mm.mysql.Driver";

            // Produces the driver needed for a JDBC connection
            try
            {
                Class.forName(driver);
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null,
                               "Couldn't find driver: " + e.getMessage());
            }

            // Attempts to make connection with driver
            Connection con = DriverManager.getConnection(url);

            // Checks the user name and password
            String query ="SELECT * FROM users WHERE username = '" + user +
                           "' AND password = '" + password + "' AND admin = '1';";

            Statement stmt = con.createStatement();

            ResultSet RS = stmt.executeQuery(query);

            if(RS.next()) // User ID and password are valid
            {
                // System started
				IDGen iDG = new IDGen();
                int tournamentID = iDG.generateID(con);
                Tournament tourny = new Tournament(theTeams, con, tournamentID);
            }
           else
            {
                // Connections and statements terminated
                stmt.close();
                con.close();

                // Show access denied dialog.
                JOptionPane.showMessageDialog(null,"Invalid userID/password");

                // Restart login process.
                Login loginScreen = new Login(theTeams);
            }

        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null, "Error:" + e.getMessage());
        }


    }
}
However when I package my classes up in a jar file the connection no longer works and I get this error...
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
Any ideas why this might be happening ? Do I need to add something similar to the
getClass().getResource()
code for getting resources, if so where do I use it ? Thanks for any ideas... Chris
Son of Cain
Son of Cain
The driver is in the classpath, right?
Also, you'll need to register it with DriverManager.

Son Of Cain
a.k.a javabeats at yahoo.ca
Whackjack
Whackjack
Replace this...

Quote:
Original post by chris_j_pook

...String driver="org.gjt.mm.mysql.Driver";// Produces the driver needed for a JDBC connectiontry{    Class.forName(driver);}...



with this...

String driver = "com.mysql.jdbc.Driver";try{    // According to MySQL documentation, a call to newInstance()    // gets around some errors in Java implementations.    Class.forName(driver).newInstance();}


The value you're putting into the driver String is deprecated. Use the newer one instead.
chris_j_pook
chris_j_pook
Quote:
Original post by Son of Cain
The driver is in the classpath, right?
Also, you'll need to register it with DriverManager.

Son Of Cain


erm...

The driver is in a folder within the project directory so that i can distribute it with the program without people having to worry about setting the classpath. Its in the folder /org/gjt/mm/mysql and seems to work fine when its not in a .jar file.

Although that might be because it uses the copy in my classpath then :/

What is driverManager ?


Thanks
Son of Cain
Son of Cain
You'll have to setup the classpath in your app manifest file in order to use the driver.

DriverManager is a class that registers JDBC drivers for your application to use. However, I was mistaken, and a call to Class.forName().newInstance() already registers the driver for you.

The setting of classpath is enough to solve your problem, but if you have to load that driver dinamically (and I doubt you need that) you'll have to use a URLClassLoader.

Son Of Cain
a.k.a javabeats at yahoo.ca

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.