import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;public class LoginGUI extends JFrame implements ActionListener{
JTextField userID=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JButton ok=new JButton("登录");
JButton regit=new JButton("注册");
JButton cancel=new JButton("取消");
JButton exit=new JButton("退出");
Connection conn;
JFrame frame=new JFrame();
public LoginGUI()
{
setTitle("用户登录");
Container c=getContentPane();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.add(new JLabel("用户名: "));
p1.add(userID);
p1.add(new JLabel("密码:"));
p1.add(password);
p2.add(ok);
p2.add(regit);
p2.add(cancel);
p2.add(exit);
c.setLayout(new GridLayout(2,1));
c.add(p1);
c.add(p2);
ok.addActionListener(this);
regit.addActionListener(this);
cancel.addActionListener(this);
exit.addActionListener(this); setSize(400,300); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:roemin", "sa", "880405" );
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from Login where userID='"+userID.getText()+"' and password='"+new String(password.getPassword())+"'");
if(rs.next()) JOptionPane.showMessageDialog(null,"登录成功!");
else JOptionPane.showMessageDialog(null,"密码或用户名不对!");
conn.close();
}catch(Exception ex){System.out.println(ex);}
}
if(e.getActionCommand().equals("注册"))
{
}
if(e.getActionCommand().equals("取消")){
userID.setText("");
password.setText("");
}
if(e.getActionCommand().equals("退出")){
System.exit(0);
}
}
public static void main(String[] s){
new LoginGUI();
}
}我做了一个登录界面连数据库的。
我又做了一个简单的注册见面
但是我不知道怎么样把两者链接起来。就是说在登录界面上一按注册按钮 就会弹出注册界面;~~~~~~~~~~~~~~谢谢!
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.sql.*;
public class RegitGUI extends JFrame implements ActionListener
{ public static void main(String[] s){
RegitGUI frame=new RegitGUI();
frame.setTitle("用户注册界面");
frame.pack();
//frame.setSize(200,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
JTextField userID=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JPasswordField repassword=new JPasswordField(10);
//添加按钮;
JButton regit=new JButton("确定");
JButton cancel=new JButton("取消");
JButton exit=new JButton("退出");
public RegitGUI()
{
Container c=getContentPane();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.add(new JLabel("用户名"));
//new JLabel("用户名").setBackground(Color.red);
p1.add(userID);
p1.add(new JLabel("用户密码"));
p1.add(password);
p1.add(new JLabel("重新确定密码"));
p1.add(repassword);
p1.setLayout(new GridLayout(3,1,2,15));
//使输入的密码用*代替;
password.setEchoChar('*');
repassword.setEchoChar('*');
//设置背景色跟前景色
//userID.setBackground(Color.yellow);
//password.setBackground(Color.blue);
//repassword.setBackground(Color.green);
//new JLabel("用户密码").setForeground(Color.yellow);
//new JLabel("重新确定密码").setForeground(Color.yellow);
regit.setBackground(Color.red);
regit.setForeground(Color.CYAN);
cancel.setBackground(Color.red);
cancel.setForeground(Color.CYAN);
exit.setBackground(Color.red);
exit.setForeground(Color.CYAN);
//版面p2添加按钮;
p2.add(regit);
p2.add(cancel);
p2.add(exit);
//设置提示文本;
regit.setToolTipText("按此键进行注册");
cancel.setToolTipText("可以取消你添的东西");
exit.setToolTipText("退出界面");
//c.setLayout(p2,BorderLayout.SOUTH);
c.setLayout(new GridLayout(2,1));
c.add(p1);
c.add(p2);
//注册;
regit.addActionListener(this);
cancel.addActionListener(this);
exit.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==regit)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:roemin", "sa", "880405" );
Statement stmt=conn.createStatement();
//ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。
ResultSet rs=stmt.executeQuery("select * from Login where userID='"+userID.getText()+"' and password='"+new String(password.getPassword())+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"此用户已经注册过了\n请重新输入用户名");
userID.setText("");
password.setText("");
repassword.setText("");
}
else
{
if(!new String(repassword.getPassword()).equals(new String(password.getPassword())))
{
JOptionPane.showMessageDialog(null,"重新确认的密码不正确\n请重新输入");
repassword.setText("");
}
else
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:roemin", "sa", "880405" );
stmt=conn.createStatement();
int rst=stmt.executeUpdate("insert into Login values('"+userID.getText()+"','"+new String(password.getPassword())+"')");
JOptionPane.showMessageDialog(null,"恭喜你注册成功!");
}
}
conn.close();
}catch(Exception ex){System.out.println(ex);}
}
else if(e.getSource()==cancel)
{
userID.setText("");
password.setText("");
repassword.setText("");
}
else if(e.getSource()==exit)
{
System.exit(0);
}
}
}
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;public class LoginGUI extends JFrame implements ActionListener{
JTextField userID=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JButton ok=new JButton("登录");
JButton regit=new JButton("注册");
JButton cancel=new JButton("取消");
JButton exit=new JButton("退出");
Connection conn;
JFrame frame=new JFrame();
public LoginGUI()
{
setTitle("用户登录");
Container c=getContentPane();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.add(new JLabel("用户名: "));
p1.add(userID);
p1.add(new JLabel("密码:"));
p1.add(password);
p2.add(ok);
p2.add(regit);
p2.add(cancel);
p2.add(exit);
c.setLayout(new GridLayout(2,1));
c.add(p1);
c.add(p2);
ok.addActionListener(this);
regit.addActionListener(this);
cancel.addActionListener(this);
exit.addActionListener(this); setSize(400,300); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:roemin", "sa", "880405" );
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from Login where userID='"+userID.getText()+"' and password='"+new String(password.getPassword())+"'");
if(rs.next()) JOptionPane.showMessageDialog(null,"登录成功!");
else JOptionPane.showMessageDialog(null,"密码或用户名不对!");
conn.close();
}catch(Exception ex){System.out.println(ex);}
}
if(e.getActionCommand().equals("注册"))
{
}
if(e.getActionCommand().equals("取消")){
userID.setText("");
password.setText("");
}
if(e.getActionCommand().equals("退出")){
System.exit(0);
}
}
public static void main(String[] s){
new LoginGUI();
}
}我做了一个登录界面连数据库的。
我又做了一个简单的注册见面
但是我不知道怎么样把两者链接起来。就是说在登录界面上一按注册按钮 就会弹出注册界面;~~~~~~~~~~~~~~谢谢!
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.sql.*;
public class RegitGUI extends JFrame implements ActionListener
{ public static void main(String[] s){
RegitGUI frame=new RegitGUI();
frame.setTitle("用户注册界面");
frame.pack();
//frame.setSize(200,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
JTextField userID=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JPasswordField repassword=new JPasswordField(10);
//添加按钮;
JButton regit=new JButton("确定");
JButton cancel=new JButton("取消");
JButton exit=new JButton("退出");
public RegitGUI()
{
Container c=getContentPane();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.add(new JLabel("用户名"));
//new JLabel("用户名").setBackground(Color.red);
p1.add(userID);
p1.add(new JLabel("用户密码"));
p1.add(password);
p1.add(new JLabel("重新确定密码"));
p1.add(repassword);
p1.setLayout(new GridLayout(3,1,2,15));
//使输入的密码用*代替;
password.setEchoChar('*');
repassword.setEchoChar('*');
//设置背景色跟前景色
//userID.setBackground(Color.yellow);
//password.setBackground(Color.blue);
//repassword.setBackground(Color.green);
//new JLabel("用户密码").setForeground(Color.yellow);
//new JLabel("重新确定密码").setForeground(Color.yellow);
regit.setBackground(Color.red);
regit.setForeground(Color.CYAN);
cancel.setBackground(Color.red);
cancel.setForeground(Color.CYAN);
exit.setBackground(Color.red);
exit.setForeground(Color.CYAN);
//版面p2添加按钮;
p2.add(regit);
p2.add(cancel);
p2.add(exit);
//设置提示文本;
regit.setToolTipText("按此键进行注册");
cancel.setToolTipText("可以取消你添的东西");
exit.setToolTipText("退出界面");
//c.setLayout(p2,BorderLayout.SOUTH);
c.setLayout(new GridLayout(2,1));
c.add(p1);
c.add(p2);
//注册;
regit.addActionListener(this);
cancel.addActionListener(this);
exit.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==regit)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:roemin", "sa", "880405" );
Statement stmt=conn.createStatement();
//ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。
ResultSet rs=stmt.executeQuery("select * from Login where userID='"+userID.getText()+"' and password='"+new String(password.getPassword())+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"此用户已经注册过了\n请重新输入用户名");
userID.setText("");
password.setText("");
repassword.setText("");
}
else
{
if(!new String(repassword.getPassword()).equals(new String(password.getPassword())))
{
JOptionPane.showMessageDialog(null,"重新确认的密码不正确\n请重新输入");
repassword.setText("");
}
else
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:roemin", "sa", "880405" );
stmt=conn.createStatement();
int rst=stmt.executeUpdate("insert into Login values('"+userID.getText()+"','"+new String(password.getPassword())+"')");
JOptionPane.showMessageDialog(null,"恭喜你注册成功!");
}
}
conn.close();
}catch(Exception ex){System.out.println(ex);}
}
else if(e.getSource()==cancel)
{
userID.setText("");
password.setText("");
repassword.setText("");
}
else if(e.getSource()==exit)
{
System.exit(0);
}
}
}
解决方案 »
- 问大家一个JAVA SE基础但又不是平凡的问题
- 如何查找数组中的数字出现的次数
- 在java中当一个Vector内的值的顺序发生变化时能不能让更外一个Vector对应的值也同步变化
- B/S中JAVA是否可以获得当前域用户的用户名
- 我要用java写一个应用程序,需要什么环境呢?
- jTable单元格相应键盘事件的问题。。
- 请教,怎么避免MalformedInputException?
- scjp求助,这道题应该选什么,以及原因,谢谢
- 问个BoxLayout的问题
- jdbc中多个连接的事务处理如何解决?我用的是sqlserver2000的jdbc驱程
- 新手求教这段代码的意思
- ireport针打不正常及如何精确控制纸长?
{
RegitGUI frame = new RegitGUI();
frame.setTitle("用户注册界面");
frame.pack();
// frame.setSize(200,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
{
new RegitGUI();
}