在mac os mavericks上将mySQL连接到java netbeans时出错

时间:2014-01-15 03:27:40

标签: java mysql swing netbeans database-connection

以下是与数据库连接的代码:

 public void database()

 {

  String temp="";

  Connection conn = null;

  PreparedStatement preparedStatement = null;


ResultSet resultSet = null;

Statement statement=null;
    try {

               String url = "jdbc:mysql://127.0.0.1:3306/golf";

       Class.forName ("oracle.jdbc.driver.OracleDriver");

       conn = DriverManager.getConnection (url,"root","");

        if (!conn.isClosed())
        {
            statement = conn.createStatement ( );
                            resultSet = statement.executeQuery("select * from golfdb");
                            writeResultSet(resultSet);
            statement.close(); 
        }
    }
    catch (Exception e)
    {

                System.err.println("Exception: " + e.getMessage());}

    finally
    {
        try
        {
            if (conn != null)
            {conn.close();}
        }
        catch (SQLException e)
        {}
    }
}

这是我运行程序时收到的错误:

Exception: oracle.jdbc.driver.OracleDriver

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

    at java.util.Vector.elementAt(Vector.java:427)

    at 

javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)

    at golf.addeditpoints(golf.java:220)

    at golf$17.actionPerformed(golf.java:728)

    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)

    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)

    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

    at 
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

    at java.awt.Component.processMouseEvent(Component.java:6414)

    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)

    at java.awt.Component.processEvent(Component.java:6179)

    at java.awt.Container.processEvent(Container.java:2084)

    at java.awt.Component.dispatchEventImpl(Component.java:4776)

    at java.awt.Container.dispatchEventImpl(Container.java:2142)

    at java.awt.Component.dispatchEvent(Component.java:4604)

    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)

    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)

    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)

    at java.awt.Container.dispatchEventImpl(Container.java:2128)

    at java.awt.Window.dispatchEventImpl(Window.java:2492)

    at java.awt.Component.dispatchEvent(Component.java:4604)

    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)

    at java.awt.EventQueue.access$400(EventQueue.java:82)

    at java.awt.EventQueue$2.run(EventQueue.java:676)

    at java.awt.EventQueue$2.run(EventQueue.java:674)

    at java.security.AccessController.doPrivileged(Native Method)

    at 
java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)

    at 
java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)

    at java.awt.EventQueue$3.run(EventQueue.java:690)

    at java.awt.EventQueue$3.run(EventQueue.java:688)

    at java.security.AccessController.doPrivileged(Native Method)

    at 
java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)

    at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)

    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)

    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)

    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

3 个答案:

答案 0 :(得分:1)

您正在尝试使用Oracle Driver连接到MYSQL。 你应该改用它:

Class.forName("com.mysql.jdbc.Driver");

当然你必须首先得到这个驱动程序的jar。 有一个很好的教程here

答案 1 :(得分:1)

您正在为数据库使用不兼容的驱动程序。如果你的数据库在mySQL中,操作系统是windows,那么你应该从这里下载jar http://dev.mysql.com/downloads/connector/j

并使用以下内容加载驱动程序

 Class.forName("com.mysql.jdbc.Driver");

这个link有一个很好的教程,可以使用Java连接到MySQL。

答案 2 :(得分:1)

在此处下载驱动程序MySQL Driver

并尝试此示例

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class JDBCExample {

  public static void main(String[] argv) {
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return;
    }

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    try {
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/suru","root", "password");
        System.out.println("Connection Success...");
    } catch (SQLException e) {
        System.out.println("Connection Failed!");
        e.printStackTrace();
        return;
    }

  }
}