JavaSwing +Mysql 的简单登陆界面实现

1.MySQL连接

​
package Swing_1_Full_edition;

import java.sql.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class MysqlUtil_Landing_interface {
	//private double width ,height ;
	private static Connection connection;
	private static Statement statement;
	private static ResultSet rs;
	public static Properties ini = null;
	public static Connection getConnection() throws Exception{
		try {
			//8.0以上用cj.
			Class.forName("com.mysql.cj.jdbc.Driver");
			//5.几
			//Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/test";
			String user="root";
			String password="123456";
			//String user = getIniKey ("UserID");
			//String password1  = getIniKey ("Password");
			connection=DriverManager.getConnection(url, user, password);
		}catch (Exception e) {
			System.out.println(e);
		}finally {
			return connection;
		}
	}
	public static void close() throws SQLException{
		//关闭是声明的反方向写
		if(rs !=null) {
			rs.close();
		}
		if(statement !=null) {
			statement.close();
		}
		if(connection !=null) {
			connection.close();
		}
	}
/*
	public static String getIniKey (String k) {
		if(!ini.containsKey (k)) {		//是否有 k 这个键
			System.out.println ("The [ " + k + " ] Key is not exist!!");
			return "";
		}//End if(!ini.containsKey (k))
		return ini.get (k).toString ();
	}*/
}

注意部分及所遇问题:

1.报错一

/*       Navicat Premium的一些运用

* java.sql.SQLException: Access denied for user 'root'@'localhost'

* (using password: YES)

* 解决: 本机设置的数据库密码是 123456 用户名:root

* 报错原因:

1.密码错误:提供的密码与数据库中 root 用户的密码不匹配。

2.用户权限问题:root 用户可能没有从 localhost 连接的权限,或者权限被撤销了。

3.MySQL用户表问题:MySQL的 user 表可能损坏或不一致,导致认证失败。

2.标准样式:

(com.mysql.jdbc.Driver)

Connectionconn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/XX”,“root”,“XXXX”);

*/

//8.0以上用cj.

Class.forName("com.mysql.cj.jdbc.Driver");

//5.几的用

//Class.forName("com.mysql.jdbc.Driver");

3.待解决

如何实现在未知 :连接名、主机名、端口、用户名、密码的条件下也能连接成功

方法是??

546701cb326e4081917bfbfbd46dd1dc.png

2.登陆界面

​
package Swing_1_Full_edition;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Landing {

	public static void main(String[] args) {
		JFrame jf =new JFrame("JF窗口(登陆窗口)");
		jf.setLayout(new FlowLayout(FlowLayout.CENTER));
		jf.setBounds(450,200,400,300);// x  y 位置  width  height  大小
		//jf.setBounds(null);			
		//标签
		JLabel jLabel1= new JLabel("账号:");
		//文本框  默认值,长度(列)
		JTextField usernameText=new JTextField("请输入账号",30);
		JLabel jLabel2=new JLabel("密码:");
		//密码框
		JPasswordField pwdText= new JPasswordField("",30);
		JTextField out=new JTextField("登陆状态",35);
		JButton button =new JButton("登陆");
		button.setSize(30, 100);
		//button.setLayout(new FlowLayout(FlowLayout.CENTER));
		pwdText.setEchoChar('*');
		
		jf.add(jLabel1);
		jf.add(usernameText);
		jf.add(jLabel2);
		jf.add(pwdText);
		jf.add(out);
		jf.add(button);
		
		
		button.addActionListener(new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String username=usernameText.getText();
				String pwd=pwdText.getText();
				try {
					//Connection connection =MysqlUtil_Landing_interface.getConnection();
					Connection connection=MysqlUtil_Landing_interface.getConnection();
					Statement statement =connection.createStatement();
					ResultSet rs =statement.executeQuery("select*from user");
					int flag=0;
					while(rs.next()) {
						if(rs.getString(2).equals(username)&&rs.getString(3).equals(pwd)) {
							flag=1;
							out.setText("Landing success!");
						}
						if(flag==0) {
							out.setText("Landing failure!");
						}
					}
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				finally {
					try {
						MysqlUtil_Landing_interface.close();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		
		
		
		/*
		button.addActionListener(new AbstractAction() {
			
			public void actionPerformed(ActionEvent e) {
				String username=usernameText.getText();
				String pwd=pwdText.getText();
					try {
					Connection connection =MysqlUtil_Landing_interface.getConnection();
					Statement statement =connection.createStatement();
					ResultSet re =executeQuery("select*from user");
				} catch (Exception exception) {
					exception.printStackTrace();
				}
				finally {
					try {
						MysqlUtil_Landing_interface.close();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		  */
		

		
		//默认true 是可缩放jf框的
		jf.setResizable(false);
		//true    return
		jf.setVisible(true);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}

}

注意事项:

1.思维先把框架写出


public class Test {

	public static void main(String[] args) {
		JFrame jf= new JFrame("登陆");
		jf.setLayout(new FlowLayout(FlowLayout.LEFT));
		jf.setBounds(460, 300, 350, 250);
        
        /*  再写具体内容          
                          */

		jf.setResizable(false);
		button.setSize(40, 20);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

	}

}

运行图:

登陆窗口

afb467786a3d4134a5208287a2ba93ce.png

登陆成功

572207dd01574db199b833efa2ae279a.png

登陆失败

153c335a19a749b395ad66ecdaf29dec.png

数据库

4c372c0f996b496c97f07d3c5b7df3f3.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值