Jdbc类设计:
连接数据库、insert_into(String name,String password)向数据库中插入数据的方法、drop(String name)删除数据的方法、update(String name,String password)修改数据方法、find()查询数据方法(注:此方法不要也行,操作中并未涉及到此方法)
Win类设计:
创建一个窗口window、一个面板contentPane,四个按钮btnNewButton,btnNewButton_1,btnNewButton_2,btnNewButton_3、两个文本框textField,textField_1、两个标签label,label1。构造方法Win()、创建窗口和面板的方法win()、以及创建按钮,文本框和标签的方法init(),然后把方法initial()和方法win(),放入构造方法中。在init()方法中对按钮设置事件监听和响应,设置Jdbc类的对象调用Jdbc类里面的方法,实现通过界面对数据库的操作。
Win1类设计:
主要是对Win类中登陆按钮的响应,显示登陆成功的页面。其中设置一个窗口frame,一个面板contentPane,一个标签lbll以及一个方法win1()实现窗口面板和标签的功能。
Menu类设计:主类,就一句话创建Win类对象,由于Win里的方法都放在了构造方法中就不用调用。
package Window;
public class Menu {
public static void main(String[] args) {
Win a = new Win();
}
}
//##################
package Window;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
public class Win1 extends JFrame {
JFrame frame = new JFrame();
private JPanel contentPane;
public void win1() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 450, 300);
frame.setVisible(true);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
frame.setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lbll = new JLabel("登陆成功");
lbll.setFont(new Font("宋体", Font.PLAIN, 30));
lbll.setForeground(new Color(0, 0, 0));
lbll.setBounds(145, 65, 131, 86);
contentPane.add(lbll);
}
}
//##############
package Window;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Win extends JFrame {
JFrame window;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
JButton btnNewButton,btnNewButton_1,btnNewButton_2,btnNewButton_3;
String name;
String password;
Jdbc jdbc = new Jdbc();
public static void main(String[] args) {
Win a= new Win();
}
public Win() {
win();
init();
}
public void win() {
window = new JFrame();
window.setBounds(100, 100, 450, 300);
window.setForeground(new Color(175, 238, 238));
window.setBackground(new Color(222, 184, 135));
window.setTitle("欢迎使用");
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setForeground(new Color(240, 255, 255));
contentPane.setBackground(new Color(0, 255, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
window.setContentPane(contentPane);
contentPane.setLayout(null);
}
void init() {
JLabel label = new JLabel("账号:");
//lblNewLabel.setBackground(new Color(0, 255, 255));
label.setBounds(76, 13, 72, 18);
contentPane.add(label);
JLabel label1 = new JLabel("密码:");
label1.setBounds(77, 50, 72, 18);//大小位置
contentPane.add(label1);
textField = new JTextField();
textField.setBounds(136, 10, 199, 24);
textField.setColumns(10);//宽度
contentPane.add(textField);
textField_1 = new JTextField();
textField_1.setBounds(136, 47, 199, 24);
textField_1.setColumns(10);
contentPane.add(textField_1);
btnNewButton = new JButton("删除用户");
btnNewButton.setBackground(new Color(127, 255, 212));
btnNewButton.setForeground(new Color(0, 0, 0));
btnNewButton.setBounds(222, 159, 113, 27);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name=textField.getText();
jdbc.drop(name);
}
});
contentPane.add(btnNewButton);
btnNewButton_1 = new JButton("修改密码");
btnNewButton_1.setBackground(new Color(127, 255, 212));
btnNewButton_1.setBounds(76, 159, 113, 27);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name=textField.getText();
password = textField_1.getText();
jdbc.update(name,password);
}
});
contentPane.add(btnNewButton_1);
btnNewButton_2 = new JButton("登陆");
btnNewButton_2.setBackground(new Color(127, 255, 212));
btnNewButton_2.setBounds(76, 117, 113, 27);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Win1 a = new Win1();
a.win1();
}
});
contentPane.add(btnNewButton_2);
btnNewButton_3 = new JButton("注册");
btnNewButton_3.setBackground(new Color(127, 255, 212));
btnNewButton_3.setBounds(222, 117, 113, 27);
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name=textField.getText();
password = textField_1.getText();
jdbc.insert_into(name,password);
}
});
contentPane.add(btnNewButton_3);
}
}
//################
package Window;
import java.util.Scanner;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Jdbc {
String sql=null;
Statement statement = null;
ResultSet rSet=null;
Connection connection = null;
String name;
String password;
public Jdbc()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
String user = "root";
String pwd = "123456";
try
{
connection = DriverManager.getConnection(url, user, pwd);
statement = connection.createStatement();
}
catch(Exception e)
{}
}
//查询数据
public void find(){
sql = "select * from user";
try {
rSet=statement.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while(rSet.next()){
System.out.println(rSet.getString(1)+","+rSet.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//插入语句
public boolean insert_into(String name,String password)
{
this.name = name;
this.password = password;
boolean b = false;
sql = "insert into user(name,password) values('"+name+"','"+password+"')";
try {
b=statement.execute(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
//删除语句
public boolean drop(String name)
{
this.name = name;
//String password = "123";
boolean b = false;
sql = "delete from user where name='"+name+"'";
try {
b=statement.execute(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("插入失败!");
}
return b;
}
//修改语句
public boolean update(String name,String password)
{
this.name = name;
this.password = password;
boolean b = false;
sql = "update user set password='"+password+"' where name='"+name+"'";
try {
b=statement.execute(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}