Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Using Mysql connector/J

  • 29-03-2006 11:42AM
    #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, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭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