Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using Mysql connector/J

Options
  • 29-03-2006 12:42pm
    #1
    Closed Accounts Posts: 999 ✭✭✭


    Hi,


    I'm trying to set up a small java app that will connect to a mysql database. I'm trying to learn how to write database applications. At the moment the code is very simple as I'm just trying to create a db connection.
    The Code:

    [PHP]
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JApplet;
    import java.applet.AppletContext;
    import java.awt.Container;
    import javax.swing.SwingConstants;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import javax.swing.JTextArea;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import org.gjt.mm.mysql.Driver;

    public class Phonebook extends JApplet
    {

    public String testConnection()
    {
    String output;

    try
    {
    try
    {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    }
    catch(ClassNotFoundException cnfe)
    {
    output = "Failed to load database driver - Class Not Found Exception";
    System.out.println("\n\n\n" + cnfe + "\n\n\n");
    return output;
    }
    catch(ExceptionInInitializerError eiie)
    {
    output = "Failed to load database driver - Exception In Initializer Error";
    return output;
    }
    catch(InstantiationException ie)
    {
    output = "Failed to load database driver - Instantiation Exception";
    return output;
    }
    catch(IllegalAccessException iae)
    {
    output = "Failed to load database driver - Illegal Access Exception";
    return output;
    }


    Connection con = DriverManager.getConnection ("jdbc:mysql://localhost/phonebook", "pbook", "L0g1n");
    output = "Connection to database successful";

    }
    catch(SQLException sqle)
    {
    output = "Failed to make database connection - SQL Exception";
    }

    return output;
    }

    public void init()
    {
    String status = "Update Pending . . .";
    Container contentPane = getContentPane();
    JPanel mainPanel = new JPanel();
    JTextArea jta = new JTextArea(status, 40, 10);

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints gbcs = new GridBagConstraints();
    mainPanel.setLayout(gridbag);
    gbcs.fill = GridBagConstraints.BOTH;
    gbcs.weightx = 1.0;
    gridbag.setConstraints(jta, gbcs);
    mainPanel.add(jta);

    status = this.testConnection();
    jta.setText(status);

    contentPane.add(mainPanel );
    }
    }[/PHP]
    When I compile and try to run this using the appletviewer I get the following error,
    java.lang.ExceptionInInitializerError
    at com.mysql.jdbc.Connection.<init>(Connection.java:1176)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
    :266)
    at java.sql.DriverManager.getConnection(DriverManager.java:525)
    at java.sql.DriverManager.getConnection(DriverManager.java:171)
    at Phonebook.testConnection(Phonebook.java:52)
    at Phonebook.init(Phonebook.java:79)
    at sun.applet.AppletPanel.run(AppletPanel.java:374)
    at java.lang.Thread.run(Thread.java:595)
    Caused by: java.lang.RuntimeException: Unable to initialize character set mappin
    g tables
    at com.mysql.jdbc.CharsetMapping.<clinit>(CharsetMapping.java:73)
    ... 8 more

    Now I've looked this bug up Here and apparently there Is a patch.
    The thing is I can't figure out from this patch what I need to do to make it work.

    Can anyone here help me out with getting this to work?


Comments

  • Registered Users Posts: 1,996 ✭✭✭lynchie


    Try using the latest snapshot of connector j. This is the development version of the driver which would have all new patches added to it.


  • Closed Accounts Posts: 999 ✭✭✭Raz


    Lynchie, you wouldn't believe how grateful I am to you. The solution provided worked a charm. Thank you so much.


Advertisement