栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

简单的登录测试案例(mysql数据库)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单的登录测试案例(mysql数据库)

写一个小例子,来进行对用户数据的查与增,没有对属性进行封装,只进行了数据库的连接与GUI界面交互。(登录注册成功没有界面显示,只在后台显示.)

package 登录;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;


public class condao {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/user_";
    static final String USER = "root";
    static final String PASS = "root123";
    public Connection init() {
    	Connection conn = null;
		// TODO Auto-generated constructor stub
    	 try
    	 {
			Class.forName(JDBC_DRIVER);
			System.out.print("数据库连接中..");
			conn=DriverManager.getConnection(DB_URL,USER,PASS);
			System.out.print("连接成功");
		} catch (Exception e)
		{
			System.out.print("连接失败");
		}
    	 return conn;
	}
  
}

 可能是我的数据库版本太低,动态执行sql语句后台总是报错,无语。

package 登录;
/*
        登录与注册  登录时对数据库数据进行比对,注册时没有进行用户查重,注册已有账户时也会失败
        
*?

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class dome extends Jframe implements ActionListener {

	JLabel name,pass;
	JTextField tname,tpass;
	JPanel panel;
	JButton L_button,A_button;
	Box baseBox,Box_1,Box_2;
	Connection con =null;
	public dome() {

		panel = new JPanel();
		name = new JLabel("用户名:");
		pass = new JLabel("密码:");
		tname = new JTextField(20);
		tpass = new JTextField(20);
		L_button = new JButton("登录");
		A_button = new JButton("注册");
		L_button.addActionListener(this);
		A_button.addActionListener(this);
		Box_1 = Box.createVerticalBox();
		Box_1.add(name);
		Box_1.add(Box.createVerticalStrut(6));
		Box_1.add(pass);
		Box_1.add(Box.createVerticalStrut(6));
		Box_1.add(L_button);
		Box_2 = Box.createVerticalBox();
		Box_2.add(tname);
		Box_2.add(Box.createVerticalStrut(6));
		Box_2.add(tpass);
		Box_2.add(Box.createVerticalStrut(6));
		Box_2.add(A_button);
		baseBox = Box.createHorizontalBox();
		baseBox.add(Box_1);
		baseBox.add(Box.createHorizontalStrut(10));
		baseBox.add(Box_2);
		panel.add(baseBox);
		add(panel,BorderLayout.CENTER);
		setSize(300, 300);
		setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String s = tname.getText().trim();
		String ss = tpass.getText().trim();
		int i = 0 ;
		// TODO Auto-generated method stub
		if(e.getActionCommand()=="登录")
		{
//	动态		String sql = "select name,password from login_user where name = ? and password = ? ";
//			PreparedStatement  ps = con.prepareStatement(sql);
//			ps.setString(1, s);
//			ps.setString(2, ss);
//			ResultSet rs  = sta.executeQuery(sql);
			String sql = "select name,password from login_user where name = '"+s+"' and password = '"+ss+"'";
			condao cd =  new condao();
			con = cd.init();
			try 
			{
				Statement sta = con.createStatement();
				ResultSet rs  = sta.executeQuery(sql);
				if(rs.next())
				{
					System.out.print("登录成功");
				}else
				{
					System.out.print("登录失败");
				}	
				con.close();
				sta.close();
				rs.close();
			} catch (Exception e1)
			{
				e1.printStackTrace();
			}
		}else
		{
			//注册
			String sql = "insert into login_user (id,name,password) values (0,'"+s+"','"+ss+"')";
			try 
			{
				condao cd =  new condao();
				con = cd.init();
//				PreparedStatement  ps = con.prepareStatement(sql);
//				ps.setString(1, s);
//				ps.setString(2, ss);
				Statement ps = con.createStatement();
				
				int rs  = ps.executeUpdate(sql);
				if(rs==1)
				{
					System.out.print("注册成功");
				}else
				{
					System.out.print("注册失败");
				}
				con.close();
				ps.close();
			} catch (Exception e1) {
				
				e1.printStackTrace();
			}
		}
	}
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号