Here is the code for the connection to the database:
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)
{}
}
}
and here is the error i receive when i run the program:
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)
You are trying to connect to MYSQL using Oracle Driver. You should use this instead:
Class.forName("com.mysql.jdbc.Driver");
Of course you have to get the jar for this driver first. There is a good tutorial here .
You are using incompatible driver for your data base. If your database is in mySQL and OS is windows than you should download jar from here http://dev.mysql.com/downloads/connector/j
and use following for loading driver
Class.forName("com.mysql.jdbc.Driver");
This link has a good tutorial for connecting to the MySQL using Java.
download driver here MySQL Driver
and try this example
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;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.