📄 logon.java
字号:
//**************************************
// Name: A Swing Logon Window
// Description:Beginners rate me on this one !! its a logon screen , its give users access to your application!! enjoy
// By: Xolani
//
//
// Inputs:username and password .....
//
// Returns:returns 1 if user details where found in the database , 0 if not
//
//Assumes:Must create a database with the table Users(UserName,Password) or must rename my odbc connection to according to his/her database ....
//
//Side Effects:your database must have the table Users(UserName,Password) .....
//This code is copyrighted and has limited warranties.
//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.3017/lngWId.2/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************
/* @ Xolani Gwaqa 2002
*(Swing) Logon Window
*The database must have Users Table as
*Users(UserName,Password)
*Also change the odbc name 'monthly' to suite you!!!!
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
public class Logon extends JFrame implements ActionListener {
public JButton bConn,bClose;
private JTextField UserName;
private JPasswordField Password;
public String sName,sPassword;
public String url ="jdbc:odbc:monthly";
public Connection conn;
public Statement stmt;
public Logon()
{
setTitle("Login Screen");
// GUI ******************
bConn = new JButton("Login");
bClose = new JButton("Close");
bConn.addActionListener(this);
bClose.addActionListener(this);
JPanel p = new JPanel();
p.add(bConn);
p.add(bClose);
JLabel lname = new JLabel("UserName");
JLabel lpassword = new JLabel("Password");
UserName = new JTextField(10);
Password = new JPasswordField(10);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2,2));
p1.add(lname);
p1.add(UserName);
p1.add(lpassword);
p1.add(Password);
p1.add(UserName);
p1.add(Password);
getContentPane().add(p1,"North");
getContentPane().add(p,"Center");
//*** End GUI***********************************
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
//set the position in the screen
int inset = 250;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset, screenSize.width - inset*2,screenSize.height-inset*2);
setSize( 300, 100);
setVisible( true );
setResizable(false);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println(e.toString());
}
try
{
conn = DriverManager.getConnection(url,"sa","");
}
catch(Exception e)
{
System.out.println("Could not load Driver");
}
}
public int CheckLogin(String UserName,String Password)
{
try
{
stmt = conn.createStatement();
//check against the database if user details do exists
ResultSet rs = stmt.executeQuery("select * from Users where Username = '" + UserName + "' and Password = '" + Password + "'");
System.out.println("Wait... ... ....");
if(rs.next())
{
return 1; // user details exists
}
else
{
return 0; // user details do not exists
}
// stmt.close();
// conn.close();
}
catch(SQLException ee)
{
System.out.println(ee.toString());
return 0;
}
}
public static void main(String args[]){
Logon theWindow =new Logon();//create new instance of Logon
theWindow.show();
}
public void actionPerformed(ActionEvent e){
String User_Name,Pass_Word;
User_Name = UserName.getText();
Pass_Word = Password.getText();
int Check;
if(e.getActionCommand().equals("Login"))
{
Check = CheckLogin(User_Name,Pass_Word);
if(Check==1)
{
//this means that your logon details where correct , then you can do anything @ this step
hide();
}
else
{
JOptionPane.showMessageDialog(null,String.valueOf("Ops! Your logon details are not correct,please try again!"));
}
}
else
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -