java链接mysql数据库实现登陆验证
mysql【users】登陆用户表如下所示未连接时源代码importjava.awt.Container;importjava.awt.Dimension;importj...
mysql【users】登陆用户表如下所示
未连接时源代码
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginWindow extends JDialog implements ActionListener {
JTextField txtUsername = new JTextField(10);
JPasswordField txtPassword = new JPasswordField(10);
JButton btnOK = new JButton("确定");
JButton btnCancel =new JButton("取消");
public LoginWindow(){
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(3,1,5,5));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
p1.add(new JLabel("用户名:"));
p1.add(txtUsername);
p2.add(new JLabel("密码:"));
p2.add(txtPassword);
p3.add(btnOK);
p3.add(btnCancel);
contentPane.add(p1);
contentPane.add(p2);
contentPane.add(p3);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
btnOK.addActionListener(this);
btnCancel.addActionListener(this);
txtUsername.addActionListener(this);
txtPassword.addActionListener(this);
setSize(250,150);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((size.width - 300) /2,(size.height - 220) / 2);
setTitle("登录窗口");
setModal(true);
setResizable(false);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnOK || e.getSource() == txtPassword) {
if (txtUsername.getText().trim().equals("wang")&& new String(txtPassword.getPassword()).equals("1234")) {
dispose();
new MainWindow();
}else{
JOptionPane.showMessageDialog(null, "用户名或密码输入错误!");
txtUsername.requestFocus();
}
}else if (e.getSource() == btnCancel) {
dispose();
System.exit(0);
}else if (e.getSource() == txtUsername) {
txtPassword.requestFocus();
}
}
public static void main(String[] args) {
JDialog.setDefaultLookAndFeelDecorated(true);
new LoginWindow();
}
}
我想改一下代码,将它变成可以通过数据库验证的界面,链接数据库方式是用纯java jdbc驱动程序的方法。
最好直接给我代码,我不太会编,谢谢~ 展开
未连接时源代码
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginWindow extends JDialog implements ActionListener {
JTextField txtUsername = new JTextField(10);
JPasswordField txtPassword = new JPasswordField(10);
JButton btnOK = new JButton("确定");
JButton btnCancel =new JButton("取消");
public LoginWindow(){
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(3,1,5,5));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
p1.add(new JLabel("用户名:"));
p1.add(txtUsername);
p2.add(new JLabel("密码:"));
p2.add(txtPassword);
p3.add(btnOK);
p3.add(btnCancel);
contentPane.add(p1);
contentPane.add(p2);
contentPane.add(p3);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
btnOK.addActionListener(this);
btnCancel.addActionListener(this);
txtUsername.addActionListener(this);
txtPassword.addActionListener(this);
setSize(250,150);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((size.width - 300) /2,(size.height - 220) / 2);
setTitle("登录窗口");
setModal(true);
setResizable(false);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnOK || e.getSource() == txtPassword) {
if (txtUsername.getText().trim().equals("wang")&& new String(txtPassword.getPassword()).equals("1234")) {
dispose();
new MainWindow();
}else{
JOptionPane.showMessageDialog(null, "用户名或密码输入错误!");
txtUsername.requestFocus();
}
}else if (e.getSource() == btnCancel) {
dispose();
System.exit(0);
}else if (e.getSource() == txtUsername) {
txtPassword.requestFocus();
}
}
public static void main(String[] args) {
JDialog.setDefaultLookAndFeelDecorated(true);
new LoginWindow();
}
}
我想改一下代码,将它变成可以通过数据库验证的界面,链接数据库方式是用纯java jdbc驱动程序的方法。
最好直接给我代码,我不太会编,谢谢~ 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
展开全部
//这是我以前写的核对数据库实现登陆的方法,你只看jdbc部分就好,我还特地给你加了点注释
String sql = "select username,password from account";
String user = request.getParameter("user");
String pass = request.getParameter("password");
int j = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCTools1.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//从表中查询获取所有账户的用户名&密码的ResultSet 对象
while(rs.next()){
int i = 0;
String username[] = new String[10];//用户名数组
String password[] = new String[10];//密码数组
username[i] = rs.getString(1);
password[i] = rs.getString(2);
if(user.equals(username[i])&&pass.equals(password[i])){//比对
response.getWriter().print("you are welcome!");
j++;
}else if(user.equals(username[i])&&!pass.equals(password[i])){
response.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n");
response.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right");
j++;
}else{
continue;
}
i++;
}
if(j == 0){
response.getWriter().println("Your username may not be properly");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools1.release(rs, ps, conn);
}
//这是我JDBCTools的getConnection方法
getConnection{
String driverClass = oracle.jdbc.driver.OracleDriver;
String jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl;
//你的数据库的用户名密码
String user = null;
String password = null;
// 通过反射创建Driver对象
Class.forName(driverClass);
return DriverManager.getConnection(jdbcUrl, user, password);}
//这是我JDBCTools的release方法
public static void release(ResultSet rs, Statement statement,
Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
String sql = "select username,password from account";
String user = request.getParameter("user");
String pass = request.getParameter("password");
int j = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCTools1.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//从表中查询获取所有账户的用户名&密码的ResultSet 对象
while(rs.next()){
int i = 0;
String username[] = new String[10];//用户名数组
String password[] = new String[10];//密码数组
username[i] = rs.getString(1);
password[i] = rs.getString(2);
if(user.equals(username[i])&&pass.equals(password[i])){//比对
response.getWriter().print("you are welcome!");
j++;
}else if(user.equals(username[i])&&!pass.equals(password[i])){
response.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n");
response.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right");
j++;
}else{
continue;
}
i++;
}
if(j == 0){
response.getWriter().println("Your username may not be properly");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools1.release(rs, ps, conn);
}
//这是我JDBCTools的getConnection方法
getConnection{
String driverClass = oracle.jdbc.driver.OracleDriver;
String jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl;
//你的数据库的用户名密码
String user = null;
String password = null;
// 通过反射创建Driver对象
Class.forName(driverClass);
return DriverManager.getConnection(jdbcUrl, user, password);}
//这是我JDBCTools的release方法
public static void release(ResultSet rs, Statement statement,
Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询