Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JDBC, Java Login Program

807591May 19 2008 — edited May 20 2008
Hello,

Im trying to create a login program which gets the username and password from a mysql database. I have 2 classes.
import java.awt.*;*
*import java.awt.event.*;
import javax.swing.*;*
*import java.sql.*;
import java.util.*;



public class Accounts extends JFrame {


public JButton getAccountButton;
public JList accountNumberList;
public Connection connection;
public JTextField accountIDText, usernameText, passwordText, tsText, activeTSText;


public static Business business;
public Accounts(Business business) {
this.business = business;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
}
private void buildGUI() {

Container c = getContentPane();
c.setLayout(new FlowLayout());









//Do Get Account Button
getAccountButton = new JButton("Get Account");
getAccountButton.addActionListener (
new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean login = false;
        String naam = usernameText.getText();
        String woord = passwordText.getText();
        login = business.checkLogin(naam, woord);
        if(login){
        setVisible(false);
        } else {
        activeTSText.setText("login failed");
        }
}
}
);




accountIDText = new JTextField(15);
usernameText = new JTextField(15);
passwordText = new JTextField(15);
tsText = new JTextField(15);
activeTSText = new JTextField(15);
JPanel second = new JPanel();
second.setLayout(new GridLayout(5,1));
second.add(getAccountButton);
second.add(accountIDText);
second.add(usernameText);
second.add(passwordText);
second.add(tsText);
second.add(activeTSText);

c.add(second);
setSize(200,200);

show();
}


public void connectToDB() {
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/***", "***", "***");



} catch(SQLException e) {
System.out.println("Unable to connect to database");
System.exit(1);
}
}





private void displaySQLErrors(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
}
private void init() {
connectToDB();
}
public static void main(String[] args) {
Accounts accounts = new Accounts(business);
accounts.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
accounts.init();
accounts.buildGUI();
}
}
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.*;*

*import java.awt.*;
import java.awt.event.*;*
*import javax.swing.*;
import java.sql.*;*
*import java.util.*;


public class Business {
    public Accounts accounts;
    private Connection connection;



    public Business() {
        accounts = new Accounts(this);
        try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
    }



    public boolean checkLogin(String name, String pass){
System.out.println("logincheck called, name="+name+" pass="+pass);



try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(
"SELECT * FROM klanten WHERE user_id = " );
if (rs.next()) {
accounts.usernameText.getText(rs.getString("username"));
accounts.passwordText.getText(rs.getInt("password"));
return true;
}
else {
return false;
}
} catch(SQLException ee) {}

}



  public void connectToDB() {
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/***", "***", "***");


} catch(SQLException e) {
System.out.println("Unable to connect to database");
System.exit(1);
}
}





private void displaySQLErrors(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
}
private void init() {
connectToDB();
}  


}
Im getting errors at the 2 getText() calls in the Business class. What im trying to do is, getting the input from the usernameText and passwordText JTextfields who are in another class(Accounts) and then checking if these inputs are equal to the inserted rows in the database table.
Why do i get these errors?
init:
deps-jar:
Compiling 1 source file to C:\winkelapplicatie2\BTSSON\build\classes
C:\winkelapplicatie2\BTSSON\src\Business.java:40: cannot find symbol
symbol  : method getText(java.lang.String)
location: class javax.swing.JTextField
accounts.usernameText.getText(rs.getString("username"));
C:\winkelapplicatie2\***\src\Business.java:41: cannot find symbol
symbol  : method getText(int)
location: class javax.swing.JTextField
accounts.passwordText.getText(rs.getInt("password"));
2 errors
BUILD FAILED (total time: 0 seconds)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 17 2008
Added on May 19 2008
36 comments
955 views