还是JDBC中的问题,麻烦在进来看下
import java.sql.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
//定义一个主界面的类
public class zhujiemian {
JFrame JJ=new JFrame();
JPanel JP=new JPanel();
JTextField user, password;
public void zong() {
JJ.setVisible(true);
JJ.add(JP);
JJ.setTitle("QQ登录程序");
JJ.setSize(500,300);
JJ.setLocation(200,100);
}
public void denglu() {
JP.setLayout(null);
JButton JB1=new JButton("登录");
JButton JB2=new JButton("退出");
JB1.setBounds(100,150,90,28);
JB2.setBounds(300,150,90,28);
JP.add(JB1);
JP.add(JB2);
JB1.addMouseListener(new JB1Listener());
JB2.addMouseListener(new JB2Listener());
}
class JB1Listener implements MouseListener {
JFrame JF=new JFrame();
JPanel JP=new JPanel();
public void mouseReleased(MouseEvent e) {
JF.setVisible(true);
JF.setTitle("QQ登录程序");
JF.setSize(200,400);
JF.setLocation(1150,30);
JJ.setVisible(false);
JF.add(JP);
JP.setLayout(null);
JButton jb1=new JButton("空间 ");
jb1.setBounds(80,60,30,28);
JP.add(jb1);
JButton jb2=new JButton("微博 ");
jb2.setBounds(130,60,30,28);
JP.add(jb2);
JLabel jb=new JLabel("未央不见");
jb.setBounds(90,0,70,70);
JP.add(jb);
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class JB2Listener implements MouseListener {
public void mouseReleased(MouseEvent e) {
System.exit(0);
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
}
public void Label() {
JLabel JL1=new JLabel("QQ号码");
JL1.setBounds(100,28,50,50);
user=new JTextField(10);
user.setBounds(165,40,150,25);
JLabel JL2=new JLabel(" 密码");
JL2.setBounds(100,68,50,50);
password=new JTextField(10);
password.setBounds(165,80,150,25);
JP.add(JL1);
JP.add(JL2);
JP.add(user);
JP.add(password);
}
//链接到数据库
public int createTable() throws Exception{
int count=0;
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/qq",
"root","a3635114");
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery("select user from qqmanager where user="+user+" and passowrd="+password);
while(rs.next()){
count++;
}
conn.close();
return count;
}
public static void main(String args[]) {
zhujiemian z=new zhujiemian();
z.Label();
z.zong();
z.denglu();
}
}
听了大神的知道,在createTable方法中建立并判断user和password。 我想返回一个值,就是如果user和password匹配,就返回1,不匹配就返回0。可是在上面JB1的鼠标事件中,无法返回值。这是怎么回事,求大神知道啊。
[解决办法]
import java.sql.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
//定义一个主界面的类
public class zhujiemian {
JFrame JJ=new JFrame();
JPanel JP=new JPanel();
JTextField user, password;
public void zong() {
JJ.setVisible(true);
JJ.add(JP);
JJ.setTitle("QQ登录程序");
JJ.setSize(500,300);
JJ.setLocation(200,100);
}
public void denglu() {
JP.setLayout(null);
JButton JB1=new JButton("登录");
JButton JB2=new JButton("退出");
JB1.setBounds(100,150,90,28);
JB2.setBounds(300,150,90,28);
JP.add(JB1);
JP.add(JB2);
JB1.addMouseListener(new JB1Listener());
JB2.addMouseListener(new JB2Listener());
}
class JB1Listener implements MouseListener {
JFrame JF=new JFrame();
JPanel JP=new JPanel();
public void mouseReleased(MouseEvent e) {
if (queryTable()) {
JF.setVisible(true);
JF.setTitle("QQ登录程序");
JF.setSize(200,400);
JF.setLocation(1150,30);
JJ.setVisible(false);
JF.add(JP);
JP.setLayout(null);
JButton jb1=new JButton("空间 ");
jb1.setBounds(80,60,30,28);
JP.add(jb1);
JButton jb2=new JButton("微博 ");
jb2.setBounds(130,60,30,28);
JP.add(jb2);
JLabel jb=new JLabel("未央不见");
jb.setBounds(90,0,70,70);
JP.add(jb);
}
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class JB2Listener implements MouseListener {
public void mouseReleased(MouseEvent e) {
System.exit(0);
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
}
public void Label() {
JLabel JL1=new JLabel("QQ号码");
JL1.setBounds(100,28,50,50);
user=new JTextField(10);
user.setBounds(165,40,150,25);
JLabel JL2=new JLabel(" 密码");
JL2.setBounds(100,68,50,50);
password=new JTextField(10);
password.setBounds(165,80,150,25);
JP.add(JL1);
JP.add(JL2);
JP.add(user);
JP.add(password);
}
//连接接到数据库
public boolean queryTable() {
boolean result=false;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/qq","root","a3635114");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select user from qqmanager where user="+user.getText()+" and passowrd="+password.getText());
if (rs.next()) {
result=true;
}
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public static void main(String args[]) {
zhujiemian z=new zhujiemian();
z.Label();
z.zong();
z.denglu();
}
}