Jump to content
  • Advertisement
Sign in to follow this  
chris_j_pook

[java] SQL connection with executable jar

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Replace this...

Quote:
Original post by chris_j_pook


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

// Produces the driver needed for a JDBC connection
try
{
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!