该登录界面是用JOptionPane弹窗接收用户名和密码,之后在数据库中搜索是否存在,最后显示登录成功与否实现
loginTest.java
package JDBC_Test;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;import javax.sql.DataSource;
import javax.swing.*;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;public class loginTest {public static void main(String[] args) throws Exception{String username,password;username = JOptionPane.showInputDialog( "输入用户名" );password = JOptionPane.showInputDialog( "输入密码" );if(isExist(username, password)){JOptionPane.showMessageDialog(null, "登录成功!", ">_<",JOptionPane.PLAIN_MESSAGE );}else {JOptionPane.showMessageDialog(null, "登录失败!", "<_>",JOptionPane.PLAIN_MESSAGE );}}public static boolean isExist(String username,String password) throws Exception{//普通连接/*String url = "jdbc:mysql://localhost:3306/lxy?useSSL=false";String user = "root";String pw = "123456";*///数据库连接池获取数据库连接//加载配置文件Properties prop = new Properties();prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);Connection conn = dataSource.getConnection();String sql = "select * from user;";PreparedStatement pstmt = conn.prepareStatement(sql);ResultSet rs = pstmt.executeQuery();while(rs.next()){String u = rs.getString("username");String p = rs.getString("password");if(u.equalsIgnoreCase(username) && p.equalsIgnoreCase(password)){conn.close();pstmt.close();return true;}}conn.close();pstmt.close();return false;}
}
druid.properties配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///lxy?useSSL=false&useServerPrepStmts=true
username=root
password=123456# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
数据库user表为