This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Applet Login via database

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
what exactly u mean when u say "it doesn't work"?
what error u get?
regards
maulin
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic