读书人

用swing mysql 写了一个QQ登录界面

发布时间: 2013-09-04 10:34:09 作者: rapoo

用swing mysql 写了一个QQ登录界面,但是功能实现不了,不能注册用户和登录验证


package com.MyQQ.test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
* @author
* 2012-4-4
*/
public class QQFrame extends JFrame {
String str1=null;
String str2=null;

JLabel label1=new JLabel("用户名:");
JLabel label2=new JLabel("密 码:");
JTextField tf1=new JTextField(10);
JTextField tf2=new JTextField(10);

JButton bt1=new JButton("登陆");
JButton bt2=new JButton("取消");
JButton bt3=new JButton("注册");
public QQFrame() {
init();

}

public void init(){
this.setTitle("QQVersion 0.1");

this.setBounds(200, 200, DEFULAT_WIDTH,DEFULAT_HEIGHT);

bt1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
str1=tf1.getText();
str2=tf2.getText();
new QQDB().check(str1, str2);
tf1.setText("");
tf2.setText("");
}
});
bt2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
bt3.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
str1=tf1.getText();
str2=tf2.getText();
new QQDB().addAccount(str1, str2);

tf1.setText("");
tf2.setText("");
}
});



JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
p1.add(label1);
p1.add(tf1);
p2.add(label2);
p2.add(tf2);
p3.add(bt1);
p3.add(bt2);
p3.add(bt3);



this.setLayout(new GridLayout(3,1));
this.add(p1);
this.add(p2);
this.add(p3);

this.pack();

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override
public void run() {

JFrame frame=new QQFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});

}
private static final int DEFULAT_WIDTH=150;



private static final int DEFULAT_HEIGHT=80;

}









package com.MyQQ.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
* @author
*2012-4-4
*/
public class QQDB {
Connection conn=null;
PreparedStatement ps=null;
ResultSet res=null;

String str1=null;
String str2=null;

String url="jdbc:mysql://localhost:3306/pwd?user=root&password=root";
public QQDB(){
init();
}
public void init(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url);
System.out.println("连接成功");

} catch (Exception e) {
e.printStackTrace();
}
}
//把从文本框里面取的值传进来和数据库里面的对比
public void check(String st1,String st2){
this.str1=st1; this.str2=st2;

try {
String sql="select userPwd from pwd where userName=str1";
ps=conn.prepareStatement(sql);
ps.execute();
res=ps.executeQuery();
if (ps.execute()) {
System.out.println("有此帐户 ");

if(str2.equals(res.getString("userPwd"))){
System.out.println("密码正确");
}else {
System.out.println("密码不正确");
}
}
conn.close();
ps.close();
res.close();
} catch (Exception e) {
e.printStackTrace();
}

}
public void addAccount(String st3,String st4){
try {
String sql2="insert into pwd(userName,userPwd) values(st3,st4)";
ps=conn.prepareStatement(sql2);
ps.executeUpdate();
System.out.println("注册成功");

} catch (Exception e) {
e.printStackTrace();
}
}

}



[解决办法]
String sql="select userPwd from pwd where userName=str1";
改成 String sql="select userPwd from pwd where userName='"+str1+"'";
[解决办法]
String sql2="insert into pwd(userName,userPwd) values(st3,st4)";
改成 String sql2="insert into pwd(userName,userPwd) values(?,?)";
ps.setString(str3);
ps.setString(str4);

[解决办法]
报错信息呢?帮你修改了一下


import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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.JPanel;
import javax.swing.JTextField;

/**
* @author
* 2012-4-4
*/
public class QQFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
String str1=null;
String str2=null;

JLabel label1=new JLabel("用户名:");
JLabel label2=new JLabel("密 码:");
JTextField tf1=new JTextField(10);
JTextField tf2=new JTextField(10);

JButton bt1=new JButton("登陆");
JButton bt2=new JButton("取消");
JButton bt3=new JButton("注册");
public QQFrame() {
init();
}

public void init(){
this.setTitle("QQVersion 0.1");

this.setBounds(200, 200, DEFULAT_WIDTH,DEFULAT_HEIGHT);

bt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {


str1=tf1.getText();
str2=tf2.getText();
new QQDB().check(str1, str2);
tf1.setText("");
tf2.setText("");
}
});

bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str1=tf1.getText();
str2=tf2.getText();
new QQDB().addAccount(str1, str2);

tf1.setText("");
tf2.setText("");
}
});


JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
p1.add(label1);
p1.add(tf1);
p2.add(label2);
p2.add(tf2);
p3.add(bt1);
p3.add(bt2);
p3.add(bt3);

this.setLayout(new GridLayout(3,1));
this.add(p1);
this.add(p2);
this.add(p3);
this.pack();
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable(){
public void run(){


JFrame frame=new QQFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
);
}
private static final int DEFULAT_WIDTH=150;
private static final int DEFULAT_HEIGHT=80;
}

class QQDB {
Connection conn=null;
PreparedStatement ps=null;
Statement stmt = null;
ResultSet res=null;
String str1=null;
String str2=null;
String url="jdbc:mysql://localhost:3306/pwd?user=root&password=root";
public QQDB(){
init();
}
public void init(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url);
System.out.println("连接成功");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//把从文本框里面取的值传进来和数据库里面的对比
public void check(String st1,String st2){
this.str1=st1; this.str2=st2;
try{
String sql="select userPwd from pwd where userName= '"+str1+"'";
stmt = conn.createStatement();//用Statement
res = stmt.executeQuery(sql);
if(res.next()){//直接判断有木有结果集


System.out.println("有此帐户");
if(str2.equals(res.getString("userPwd"))){
System.out.println("密码正确");
}else{
System.out.println("密码不正确");
}
}
} catch (SQLException e){
e.printStackTrace();
}
finally{
try {
stmt.close();
res.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}

}
}

public void addAccount(String str3,String str4){

String sql2="insert into pwd(userName,userPwd) values(?,?)";
try {
ps=conn.prepareStatement(sql2);
ps.setString(1, str3);//跟数据库类型相对应
ps.setString(2, str4);
int resultInt = ps.executeUpdate();
if(resultInt >= 0)System.out.println("注册成功");//一般不报错就能成功,可以不加判断。
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}

}

}

}


读书人网 >J2SE开发

热点推荐