java关于验证登陆的问题
本帖最后由 Waitxl 于 2013-10-18 11:23:54 编辑 出现了几句错误,今天刚刚学连接数据库,请大神指教指教,先谢谢了。
java jdbc
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.lang.Object.*;
import java.util.*;
import java.sql.*;
public class DengLu extends JFrame implements ActionListener{
public static void main(String[] args){
new DengLu();
//i.t();
}
JButton b1=new JButton("登陆");
JLabel l1=new JLabel("用户名");
JLabel l2=new JLabel("密码");
JTextField t1=new JTextField(10);
JTextField t2=new JTextField(10);
String id=null;
String password=null;
String passwordD;
public int screanWidth1,screanHeight1;
public DengLu()
{
Toolkit tool=getToolkit();
Dimension dim=tool.getScreenSize();
screanWidth1=dim.width;
screanHeight1=dim.height;
setTitle("超市管理系统登录界面");
setLayout(null);
add(b1);
add(l1);
add(l2);
add(t1);
add(t2);
l1.setBounds(130,45,50,40);
t1.setBounds(210,55,100,25);
l2.setBounds(130, 120, 50,40);
t2.setBounds(210,130,100,25);
b1.setBounds(210, 200, 60, 20);
b1.addActionListener(this);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(dim.width/3,dim.height/3,500,300);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1) {
id=t1.getText();
password=t2.getText();
Connection con;
Statement sql;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:SuperMaket";
con=DriverManager.getConnection(url);
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM CustomMessage where ID='"+id+"'");
while(rs.next())
{
passwordD=rs.getString(2);
}
// persontyD=rs.getString(3);}
con.close();
}
catch?(SQLException g)
{
System.out.println(g);
}
if(password.equals(passwordD))
{
new ZhuJieMian();
this.setVisible(false);
}
else
{
JOptionPane.showMessageDialog(this,"帐户或密码错误\n请重新输入","提示",JOptionPane.WARNING_MESSAGE);
}
}
}
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class DengLu extends JFrame implements ActionListener {
public static void main(String[] args) {
new DengLu();
// i.t();
}
JButton b1 = new JButton("登陆");
JLabel l1 = new JLabel("用户名");
JLabel l2 = new JLabel("密码");
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);
String id = null;
String password = null;
String passwordD;
public int screanWidth1, screanHeight1;
public DengLu() {
Toolkit tool = getToolkit();
Dimension dim = tool.getScreenSize();
screanWidth1 = dim.width;
screanHeight1 = dim.height;
setTitle("超市管理系统登录界面");
setLayout(null);
add(b1);
add(l1);
add(l2);
add(t1);
add(t2);
l1.setBounds(130, 45, 50, 40);
t1.setBounds(210, 55, 100, 25);
l2.setBounds(130, 120, 50, 40);
t2.setBounds(210, 130, 100, 25);
b1.setBounds(210, 200, 60, 20);
b1.addActionListener(this);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(dim.width / 3, dim.height / 3, 500, 300);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
id = t1.getText();
password = t2.getText();
Connection con;
Statement sql;
ResultSet rs;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:SuperMaket";
con = DriverManager.getConnection(url);
sql = con.createStatement();
rs = sql.executeQuery("SELECT * FROM CustomMessage where ID='"
+ id + "'");
while (rs.next()) {
passwordD = rs.getString(2);
}
// persontyD=rs.getString(3);}
con.close();
} catch (SQLException g) {
System.out.println(g);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (password.equals(passwordD)) {
new ZhuJieMian();
this.setVisible(false);
} else {
JOptionPane.showMessageDialog(this, "帐户或密码错误\n请重新输入", "提示",
JOptionPane.WARNING_MESSAGE);
}
}
}
}