0

I made a program that makes a simple GUI for a login form. Just for testing purposes I made it so when you click the "Login" button, Java should just print out the query results from MySQL database. In MySQL I have a database schema called "test" and a table called "login". The login table only has 1 row: "1, angelo, password" under the columns: loginID, Username and Password.

MYSQL Username is root. Host is localhost port 3306. MySQL Server is currently running.

I have the "mysql-connector-java-5.1.25-bin" in my "Extra Classpath" folder for DrJava (that's the software I'm using to program).

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

( CODE FOR THE GUI GOES HERE)

                Connection connection = null;

try {
    // Load the JDBC driver
    String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
    Class.forName(driverName);

    // Create a connection to the database
    String serverName = "localhost:3306";
    String mydatabase = "test";
    String url = "jdbc:mysql://" + serverName +  "/" + mydatabase; // a JDBC url
    String username = "root";
    String password = "";
    connection = DriverManager.getConnection(url, username, password);

                    Statement st = connection.createStatement();
                ResultSet rs = st.executeQuery("select * from login");

                while(rs.next()) {
                    System.out.println( rs.getString("username"));
                }
                st.close();
                rs.close();
                connection.close(); 


} catch (ClassNotFoundException e) {
    // Could not find the database driver
} catch (SQLException e) {
    // Could not connect to the database
}

Whenever I run this there are no errors, it's just that nothing prints out.

EDIT: WHen I add e.printStackTrace I get this error:

java.lang.ClassNotFoundException
at edu.rice.cs.plt.reflect.PathClassLoader.findClass(PathClassLoader.java:148)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at swing_sample$handler.actionPerformed(swing_sample.java:108)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Based on what I posted can you tell me what I'm doing wrong? Thanks.

Angelo Rivera
  • 191
  • 1
  • 2
  • 7

4 Answers4

1

you seem to have using the row data to print in the following statement :

 System.out.println( rs.getString("angelo"));

you need to give the column name or column index to rs.getString()

for example:

 System.out.println( rs.getString("user_name"));

or

 System.out.println( rs.getString(1));// assuming what you are lookin for is found in the first column
codeMan
  • 5,730
  • 3
  • 27
  • 51
0
  1. try printing the url after setting the variable url It shld look something like: jdbc:mysql://localhost:3306/dbName

  2. As pointed out by codeMan, please use the column name instead of data, in your case replace angelo with Username.

passion
  • 44
  • 1
0

problem is with loading JDBC Driver... same error occurs in my application when i don't add mysql-connector.jar to my build path. It seems your classpath is not set correctly.. have you added mysql-connector.jar to build path?

Michael Shrestha
  • 2,547
  • 19
  • 30
  • I have "mysql-connector-java-5.1.25-bin.jar" in my folder for the "Extra Classpath" in DrJava. Is there something else I have to do? (like edit Build Path in the code or somewhere else?) – Angelo Rivera Jun 24 '13 at 05:30
  • your code works for me when i add mysql-connector.jar ... are u sure your mysql-connector-java-5.1.25-bin is jar file and not zip file... if it's zip file then first unzip it and give the location of jar file to your Extra Classpath – Michael Shrestha Jun 24 '13 at 05:40
0

your code is looks correct, i thing your code try to load some of classes which is not in your class, make sure all dependency class should be in your class path.

clean code and build your project .

it might solve your problem solve your issue.

KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52