login
package addresslist; import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent; import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; /*
* 封装的太差啦!
* 要好好理解继承封装多态的使用
* 不然代码就没法看了
* 其实是后期修改太困难啦
* 通用模版的使用
* 代码的可复用性
* 考虑好了之后再动手敲代码
* 不然写了维护起来也比较困难最后只好放弃
*
* 然后就是代码的重构与优化
*
*/
public class login extends JFrame { private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
public Judge judge=new Judge();
static String username;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame fra = new login();
fra.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public void setall(String n,String p){
textField.setText(n);
passwordField.setText(p);
} public login() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new NewPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane); JLabel label = new JLabel("\u7528\u6237\u540D"); textField = new JTextField();
textField.setColumns(10); JLabel label_1 = new JLabel("*");
textField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
try {
String s=textField.getText().toString();
if(!judge.exist(s)){
label_1.setText("no such nickname!");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void focusGained(FocusEvent e) {
label_1.setText("*");
}
}); JLabel label_2 = new JLabel("\u5BC6\u7801"); passwordField = new JPasswordField(); JButton btnLogin = new JButton("login");
btnLogin.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==10){
btnLogin.doClick();
}
}
}); btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=new String(passwordField.getPassword());
String n=textField.getText().toString();
try {
if(judge.canlogin(n,s)){
username=n;
mainFrame frame=new mainFrame();
frame.setVisible(true);
dispose();
}
else{
JOptionPane.showMessageDialog(null, "密码或用户名错误");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}); JButton btnRegister = new JButton("register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
register rs=new register();
rs.setVisible(true);
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(66)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label)
.addComponent(label_2))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(passwordField)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
.addGap(28)
.addComponent(label_1))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(80)
.addComponent(btnLogin)
.addGap(37)
.addComponent(btnRegister)))
.addContainerGap(151, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(48)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(label_1)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(29)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(48)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnLogin)
.addComponent(btnRegister))
.addContainerGap(61, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
}
class NewPanel extends JPanel{
public NewPanel() {} public void paintComponent(Graphics g)
{
int x=0,y=0;
java.net.URL imgURL=getClass().getResource("login.jpg"); //test.jpg是测试图片,与Demo.java放在同一目录下
ImageIcon icon=new ImageIcon(imgURL);//this is really useful
g.drawImage(icon.getImage(),x,y,getSize().width,getSize().height,this);
while(true)
{
g.drawImage(icon.getImage(),x,y,this);
if(x>getSize().width && y>getSize().height)break;
//这段代码是为了保证在窗口大于图片时,图片仍能覆盖整个窗口
if(x>getSize().width)
{
x=0;
y+=icon.getIconHeight();
}
else
x+=icon.getIconWidth();
}
}
}
}
package addresslist; import java.awt.BorderLayout;
import java.awt.EventQueue; import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton; import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent; public class register extends JFrame { private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private JPasswordField passwordField_1;
public Judge judge=new Judge(); /**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
register frame = new register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public register() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane); JLabel label = new JLabel("\u7528\u6237\u540D"); textField = new JTextField();
textField.setColumns(10); JLabel label_1 = new JLabel("*");
textField.addFocusListener(new FocusAdapter() {
@Override public void focusGained(FocusEvent e) {
label_1.setText("*");
}
@Override
public void focusLost(FocusEvent e) {
try {
if(!judge.exist(textField.getText().toString())){
label_1.setText("is valiable");
}
else{
label_1.setText("the name is used!");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}); JLabel label_2 = new JLabel("\u5BC6\u7801"); passwordField = new JPasswordField(); JLabel label_3 = new JLabel("\u786E\u8BA4\u5BC6\u7801"); passwordField_1 = new JPasswordField();
JLabel label_4 = new JLabel("*");
passwordField_1.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
label_4.setText("*");
}
@Override
public void focusLost(FocusEvent e) {
if(new String(passwordField.getPassword()).equals(new String(passwordField_1.getPassword()))){
label_4.setText("*");
}
else{
label_4.setText("the password is not the same,please check it!");
}
}
}); JButton btnRegister = new JButton("register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { try {
if(!judge.exist(textField.getText().toString())){
String n=textField.getText().toString();
String p=new String(passwordField.getPassword());
judge.regist(n,p);
login lg=new login();
lg.setVisible(true);
lg.setall(n,p);
dispose();
}
else{
JOptionPane.showMessageDialog(null, "the name is exist please try another!");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} });
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(39)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(btnRegister)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(47)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(label_2)
.addComponent(label))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
.addComponent(passwordField)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(39)
.addComponent(label_3)
.addGap(18)
.addComponent(passwordField_1))))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label_1)
.addComponent(label_4))
.addContainerGap(199, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(46)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(26)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_3)
.addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_4))
.addGap(34)
.addComponent(btnRegister)
.addContainerGap(41, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
} }
package addresslist; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; public class Judge {
public static void main(String[] args) throws Exception {
String url ="jdbc:mysql://localhost:3306/login"; //数据库连接字符串
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
Connection conn= (Connection) DriverManager.getConnection(url,"tan","tn5201314"); //建立连接
Statement stmt=(Statement) conn.createStatement(); //创建SQL容器
String sql="select * from user"; //表为teacher
ResultSet rs=stmt.executeQuery(sql); //获得结果集 while( rs.next() ) { //处理结果集
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("password")+"\n");
}
rs.close(); stmt.close(); conn.close(); //关闭次序 } public Connection getConnection()throws Exception{
String url ="jdbc:mysql://localhost:3306/login"; //数据库连接字符串
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
Connection conn= DriverManager.getConnection(url,"tan","tn5201314");
return conn;
}
public boolean exist(String s) throws Exception{
Connection conn= getConnection(); //建立连接
Statement stmt=conn.createStatement(); //创建SQL容器
String sql="select * from user where name='"+s+"'"; //表为user
ResultSet rs=stmt.executeQuery(sql); //获得结果集
if( rs.next() ) {
rs.close(); stmt.close(); conn.close();
return true;
}
else
{
rs.close(); stmt.close(); conn.close();
return false;
}
//关闭次序 } public boolean canlogin(String n,String p) throws Exception{
Connection conn=getConnection(); //建立连接
Statement stmt=conn.createStatement(); //创建SQL容器
String sql="select password from user where name='"+n+"'"; //表为user
ResultSet rs=stmt.executeQuery(sql); //获得结果集
if(rs.next()){
if(rs.getString("password").equals(p)){
rs.close(); stmt.close(); conn.close();
return true;
}
else{
rs.close(); stmt.close(); conn.close();
return false;
}
}
else{
rs.close(); stmt.close(); conn.close();
return false;
} } public void regist(String n,String p) throws Exception{
Connection conn=getConnection(); //建立连接
Statement stmt=conn.createStatement(); //创建SQL容器
String sql="insert into user values('"+n+"','"+p+"')"; //表为user
stmt.executeUpdate(sql);
stmt.close(); conn.close();
}
}
login的更多相关文章
- 浅谈SQL注入风险 - 一个Login拿下Server
前两天,带着学生们学习了简单的ASP.NET MVC,通过ADO.NET方式连接数据库,实现增删改查. 可能有一部分学生提前预习过,在我写登录SQL的时候,他们鄙视我说:“老师你这SQL有注入,随便都 ...
- 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?
p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...
- Day01 login module
知识点:模块导入 变量赋值的两种形式 格式化输出 for循环 if...else 嵌套 #!C:\Program Files\Python35/bin # -*- conding:utf-8 ...
- Login控件尝试
新建web项目,添加default.aspx.Register.aspx.Login.aspx. default.aspx中添加LoginName.LoginStatus,LoginName的Form ...
- [转载] 构造linux 系统下免密码ssh登陆 _How to establish password-less login with SSH
In present (post production) IT infrastructure many different workstations, servers etc. have to be ...
- Security9:查询Login被授予的权限
在给一个Login授予权限时,发现该Login已经存在,其对应的User也存在于指定的DB中,查看该Login在指定DB中已被授予的权限. 1,查看Login的Server PrincipalID s ...
- php中的登陆login
Login <?php require "../include/DBClass.php"; $username=$_POST['UserName']; $password=$ ...
- MS SQL Could not obtain information about Windows NT group/user 'domain\login', error code 0x5. [SQLSTATE 42000] (Error 15404)
最近碰到一个有趣的错误:海外的一台数据库服务器上某些作业偶尔会报错,报错信息如下所示: -------------------------------------------------------- ...
- MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems
早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...
- Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken
在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...
随机推荐
- EXCEL里面的数字显示为文本 不用科学计数法显示
1. 在输入这一串数字前加撇号“'”(英文状态下的单引号)即可.2. 先将这一列设置为“文本”格式,然后直接输入这一串数字即可. 已经输入好了数字,那估计你这些数字的后三位都已经全变成“0”了,用 ...
- pdf2swf , xpdf 部分用法
http://hi.baidu.com/abpsoft/item/1d1eb0f50c9d1fd86225d2c0 pdf2swf详细参数使用说明 官方地址:http://www.swftools.o ...
- python的各种编辑器-PyScripter、pycharm 、atom、vscode、Sublime Text等等
RT,本文主要列举python的各种编辑器-PyScripter.pycharm .atom.vscode.Sublime Text等等. PyScripter 开源 免费 windows only ...
- MVC 本地运行可以发布到IIS 报Sorry, an error occurred while processing your request.解决方案
发布MVC程序的时候经常遇到这种情况,每次都要搞好久才找到问题.最终找到解决办法: 报500错误大部分的情况还是程序存在异常,但全部被MVC错误拦截成了友好提示, 只要在Web.config下添加一行 ...
- 遗传算法的C语言实现(二)-----以求解TSP问题为例
上一次我们使用遗传算法求解了一个较为复杂的多元非线性函数的极值问题,也基本了解了遗传算法的实现基本步骤.这一次,我再以经典的TSP问题为例,更加深入地说明遗传算法中选择.交叉.变异等核心步骤的实现.而 ...
- Linux下PHP的完全卸载
如果想把PHP彻底的卸载干净,直接用yum的remove命令是不行的,而需要查看有多少rpm包,然后按照依赖顺序逐一卸载,在网上查了好多,都是通过 "rpm -qa | grep php& ...
- hadoop2的高可用性
1 hadoop2 namenode由一个节点变成两个节点,同时在线,且同时只有一个是活跃的,如果一个出了问题,另外一个立即接替:没必要配置Secondary NameNode.Checkpoi ...
- c++ boost (递归)遍历目录
c++ 终于有办法跨平台访问文件系统了,虽然还是要借助boost.不多说,上代码 /** visitdir.cpp **/#include <iostream> #include < ...
- commitizen-规范commit-message
安装指南 安装commitizen sudo npm install -g commitizen 配置 cd到.git所在目录 commitizen init cz-conventional-chan ...
- hibernate 中createQuery与createSQLQuery两个用法
hibernate 中createQuery与createSQLQuery两者区别是:前者用的hql语句进行查询,后者可以用sql语句查询前者以hibernate生成的Bean为对象装入list返回后 ...