导入jdbc的包
查了查网上的资料,发现导入jdbc的jar包十分繁琐,我就想到了maven。
maven是个十分方便的包管理平台,既然导入jar如此麻烦,使用它会便捷很多。
创建个带maven的web项目。
打开里面的pom.xml进行导入包。
<dependency>
<groupId>net.jahhan</groupId>
<artifactId>jdbc-api</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
这个是jdbc的jar包,直接写在maven中。
然后刷新一下maven,jar就会自动下载安装。
先创建mysql数据库(这里已经提前创建好了)
`后面是注册和登录模块的
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class register {//注册
static String username;
static String passwords;
public static String getUsername() {
return username;
}
public static String getPassword() {
return passwords;
}
public register(){
JFrame jf=new JFrame("注册界面");
final JPanel jPanel=new JPanel();
jf.setBounds(300,300,300,120);
jPanel.setBounds(300,300,300,300);
jPanel.setBackground(Color.darkGray);
final JTextField jt1=new JTextField("账号",20);
final JTextField jt2=new JTextField("密码",20);
JButton jButton=new JButton("注册");
jPanel.add(jt1,BorderLayout.CENTER);
jPanel.add(jt2,BorderLayout.CENTER);
jPanel.add(jButton);
jf.add(jPanel);
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String _username=jt1.getText();
String _password=jt2.getText();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
PreparedStatement ps=null;
PreparedStatement ps1=null;
String url="jdbc:mysql://localhost:3306/SQLSystem?useSSL=false&serverTimezone=UTC";
String user="root";
String password="ccx425273580";
Connection conn = DriverManager.getConnection(url,user,password);
String sql="insert into SQLSystem(user,password) values(?,?)";
String sql1="select user from SQLSystem";
ps=conn.prepareStatement(sql);
ps1= conn.prepareStatement(sql1);
ps.setString(1,_username);
ps.setString(2,_password);
ResultSet rs= ps1.executeQuery();
while(rs.next()){
String s=rs.getString("user");
if (s.equals(_username)){
JOptionPane.showConfirmDialog(jPanel,"账号已经存在","提示",0);
}else{
ps.executeUpdate();
JOptionPane.showConfirmDialog(jPanel,"注册成功","提示",0);
}
}
ps.close();
conn.close();
} catch (ClassNotFoundException | SQLException ex) {
JOptionPane.showConfirmDialog(jPanel,"账号已经存在","提示",0);
}
}
});
}
public static void main(String[] args) {
register register=new register();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class sign {//登陆
public sign(){
JFrame jf=new JFrame("登陆界面");
final JPanel jPanel=new JPanel();
jf.setBounds(300,300,300,120);
jPanel.setBounds(300,300,300,300);
jPanel.setBackground(Color.darkGray);
final JTextField jt1=new JTextField("账号",20);
final JTextField jt2=new JTextField("密码",20);
JButton jButton=new JButton("登陆");
jPanel.add(jt1,BorderLayout.CENTER);
jPanel.add(jt2,BorderLayout.CENTER);
jPanel.add(jButton);
jf.add(jPanel);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String _user=jt1.getText();
String _password=jt2.getText();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/SQLSystem?useSSL=false&serverTimezone=UTC";
String user="root";
String password="ccx425273580";
Connection conn = DriverManager.getConnection(url,user,password);
String sql="select password from SQLSystem where user=?";
String sql1="select user from SQLSystem";
PreparedStatement ps=conn.prepareStatement(sql);
PreparedStatement ps1=conn.prepareStatement(sql1);
ps.setString(1,_user);
ResultSet rs=ps.executeQuery();
ResultSet rs1= ps1.executeQuery();
int count=0;
while(rs1.next()){
String user1=rs1.getString("user");
if (user1.equals(user)){
JOptionPane.showConfirmDialog(jPanel,"登陆成功","提示",0);
}else{
count=1;
}
}
if (count == 1) {
JOptionPane.showConfirmDialog(jPanel,"账号或密码错误","提示",0);
}
while(rs.next()){
String password1=rs.getString("password");
if (password1.equals(_password)){
JOptionPane.showConfirmDialog(jPanel,"登陆成功","提示",0);
}else{
JOptionPane.showConfirmDialog(jPanel,"账号或密码错误","提示",0);
}
}
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
}
}
});
}
public static void main(String[] args) {
sign sign=new sign();
}
}
下面是测试。
第一个版本可能还有些许bug,大家指正出来,修订一下。