JAVA 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) {
int i;
shujuku();//链接数据库
i=validate();//测试是否相等
//if (JT1.getText().equals("name")&&JT2.getText().equals("pw"))
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);
}
//判断 user 与 password是否在数据库中,并匹配user与password
public int validate(){
return 0;
}
//链接到数据库
public void shujuku(){
}
public static void main(String args[]) {
zhujiemian z=new zhujiemian();
z.Label();
z.zong();
z.denglu();
}
}
就是在validate()这个方法里面 计算user与pssword是否匹配;
直接说算法就好 。然后我自己写代码。谢谢啦!!!!!
[解决办法]
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Administrator
*/
public abstract class Database {
private static Connection connection = null;
/**
* 连接数据库
*/
public static Connection getConnect() throws SQLException, Exception {
if (connection != null && connection.isValid(1)) {
return connection;
}
// URL指向要访问的数据库名jdbc_test
String url = Config.getConfig("database.url");
// MySQL配置时的用户名
String user = Config.getConfig("database.user");
// Java连接MySQL配置时的密码
String password = Config.getConfig("database.password");
//这句可以没有
DriverManager.registerDriver(new Driver());
// 连接数据库
connection = DriverManager.getConnection(url, user, password);
connection.setAutoCommit(true);
//设置数据库的字符集
Statement statement = connection.createStatement();
statement.execute("SET character_set_client=`" + Config.getConfig("database.character_set_client") + "`");
statement.execute("SET character_set_connection=`" + Config.getConfig("database.character_set_connection") + "`");
statement.execute("SET character_set_results=`" + Config.getConfig("database.character_set_results") + "`");
statement.close();
//
return connection;
}
}
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import Database;
PreparedStatement ps = Database.getConnect().prepareStatement("select * from student order by id asc");
// 结果集
ResultSet resultSet = ps.executeQuery();
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println("id" + "\t" + "content");
System.out.println("-----------------");
while (resultSet.next()) {
// 选择sno,sname两列数据
System.out.println(resultSet.getString("id") + "\t" + resultSet.getString("content"));
}
int count=0;//为什么validate要返回int?
class.forName("");//加载驱动类
Connection con=DriverManager.getConnection("","","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select user from table where user="+user+" and passowrd="+password);
while(rs.next()){
//有结果
count++;
}
con.close();
return count;
"select user from table where user="+user+" and passowrd="+password