File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Applet Login via database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Applet Login via database" Watch "Applet Login via database" New topic
Author

Applet Login via database

Aqualyn Thomas
Greenhorn

Joined: Jun 27, 2003
Posts: 5
i have created a login applet and i try to connect to my database using JDBC-ODBC...
however i couldn't connect to the database

my codes.....can anyone give me the solution pls

// Java core packages
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JApplet;
// Java extension packages
import javax.swing.*;
public class TestingLogin extends JApplet
{
private JTextField nameField;
private JPasswordField passwordField;
private JButton loginButton, exitButton;
private JLabel label, label2;
MyJPanel panel = new MyJPanel();

public TestingLogin()
{
//super( "TestingLoginPage" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );

// construct textfield with default sizing
label = new JLabel("Name");
container.add(label);
nameField = new JTextField( 10 );
container.add( nameField );

// construct textfield with default text
label2 = new JLabel("Password");
container.add(label2);
passwordField = new JPasswordField( 10 );
container.add( passwordField );
// register event handlers
TextFieldHandler handler = new TextFieldHandler();
nameField.addActionListener( handler );
passwordField.addActionListener( handler );
// create login button
loginButton = new JButton( "Login" );
container.add( loginButton );
// create exit button
exitButton = new JButton( "Exit" );
container.add( exitButton );

// create an instance of inner class ButtonHandler
loginButton.addActionListener( handler );
exitButton.addActionListener( handler );

setSize( 350, 100 );
setVisible( true );
}
// execute application
public static void main( String args[] )
{
TestingLogin application = new TestingLogin();
// application.setDefaultCloseOperation( JApplet.EXIT_ON_CLOSE );
}
//class to display an ImageIcon on a panel
class MyJPanel extends JPanel
{
private ImageIcon imageIcon;
public MyJPanel()
{
imageIcon = new ImageIcon( "nyp.gif" );
}
}
// private inner class for event handling
private class TextFieldHandler implements ActionListener
{
// process text field events
public void actionPerformed( ActionEvent event )
{
//**************************************************************************************
if (event.getSource()==login)
{
String url = "jdbc dbc:Staffs";
String nameField = "STAFFID";
String passwordField = "PASSWORD";
name=username.getText();
pass=password.getText();
if ( user.length()==0 || pass.length()==0 )
{
JOptionPane.showMessageDialog( null, "Please fill in all values", "Login Error", JOptionPane.ERROR_MESSAGE);
}
else
{
String sql="SELECT Count(*) AS RStotal FROM STAFF WHERE Username='" + user + "' AND Password='" + pass + "'";
String url="jdbc dbc:echo";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url);
Statement login = conn.createStatement();
ResultSet rs = login.executeQuery(sql);
while (rs.next())
{
count = rs.getInt("RStotal");
if(count!=0)
{
setVisible(false);
parent.setVisible(true);
}
else
{
JOptionPane.showMessageDialog( null, "Invalid Username or Password", "Login Error", JOptionPane.ERROR_MESSAGE);
}
}
rs.close();
login.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
//************************************************************************************************
String string = "";
// user pressed Enter in JTextField nameField
if ( event.getSource() == nameField )
string = "nameField: " + event.getActionCommand();

// user pressed Enter in JTextField passwordField
else if ( event.getSource() == passwordField )
{
JPasswordField pwd =
( JPasswordField ) event.getSource();
string = "passwordField: " + new String( passwordField.getPassword() );
}
JOptionPane.showMessageDialog( null,"You pressed: " + event.getActionCommand() );
}
}
}
Thanks in advance
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi,
what exactly u mean when u say "it doesn't work"?
what error u get?
regards
maulin
Torsten Schippel
Ranch Hand

Joined: May 09, 2003
Posts: 62
Hi Aqualyn,
here it is:

I've made some changes, first I removed the TextFieldHandler and set the applet as handler. It's just easier access to the applet components.
Next "cannot resolve symbol" means the system doesn't know the type or it can't use it. So you need to declare name and pass as String like sql (former posting). Additional I made some ohter small changes. Try to understand it
BTW, MyJPanel is never used.
It's now working with ODBC source Staffs and table named STAFF.
Regards
Torsten
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet Login via database
 
Similar Threads
Applet Login via database
login page
Help!!!!!! GridBag Layout constraints
[SOLVED] must implement the inherited abstract method
Login page