主函数
注意看代码的断句,太多了我没有全部整理好。
import geng.view.RegisterView;
import java.sql.*;
public class Bookkk{
public static void main(String[] args) {
MainWindow window=new MainWindow();
window.setTitle(“登录界面”);
}
}
import java.sql.*;
public class GetDBConnection {
public static Connection connectDB(String DBName,String id,String password) {
try {
Class.forName(“com.mysql.cj.jdbc.Driver”);
}
catch (Exception e) {
System.out.println(“666666666666666666666”);
}
Connection con = null;
String uri =“jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC”;
try {
con = DriverManager.getConnection(uri,id,password);
}
catch (SQLException e) {
System.out.println(e);
}
return con;
}
}
import geng.view.RegisterAndLoginView;
import geng.view.RegisterView;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainWindow extends JFrame {
JButton computerButton;
RegisterAndLoginView view;
MainWindow() {
view = new RegisterAndLoginView();
add(view, BorderLayout.CENTER);
add(computerButton, BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
geng包
handle.
package geng.handle;
import FuView.Windowapp;
import UserView.WindowUser;
import geng.model.Login;
import geng.view.LoginView;
import geng.view.RegisterAndLoginView;
import geng.view.RegisterView;
import javax.swing.;
import java.awt.;
import java.sql.*;
public class HandleLogin extends Frame{
LoginView loginView;
Connection con;
PreparedStatement preSql;
ResultSet rs;
public HandleLogin(){
try {
Class.forName(“com.mysql.cj.jdbc.Driver”);
}
catch (Exception e) {
System.out.println(“666”);
}
con = null;
String uri =“jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC”;
try {
con = DriverManager.getConnection(uri,“root”,"");
}
catch (SQLException e) {
System.out.println(“错误”);
}
}
public Login queryVerify(Login loginModel){
String id= loginModel.getID();
System.out.println(id);
String password= loginModel.getPassword();
String ganli=“guanliyuan”;
String sqlStr=“select id,password from register where id =? and password=?”;
try {
preSql=con.prepareStatement(sqlStr);
preSql.setString(1,id);
preSql.setString(2,password);
rs=preSql.executeQuery();
if(rs.next()==true){//登录成功
if (id.equals(ganli)){
JOptionPane.showMessageDialog(null,“登录成功”,“恭喜”,JOptionPane.WARNING_MESSAGE);
Windowapp windowapp = new Windowapp();
windowapp.setTitle(“管理界面”);
}
else {
JOptionPane.showMessageDialog(null,"登录成功","恭喜",JOptionPane.WARNING_MESSAGE);
WindowUser user = new WindowUser();
}
}
else {
JOptionPane.showMessageDialog(null,"账号或密码错误","请重新登录",JOptionPane.WARNING_MESSAGE);
}
con.close();
}
catch (SQLException e){}
return loginModel;
}
}
package geng.handle;
import geng.model.Register;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class HandleRegister {
Connection con;
PreparedStatement preSql;
public HandleRegister(){
try {
Class.forName(“com.mysql.cj.jdbc.Driver”);
}
catch (Exception e) {
System.out.println(“666”);
}
con = null;
String uri =“jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC”;
try {
con = DriverManager.getConnection(uri,“root”,"");
}
catch (SQLException e) {
System.out.println(“错误”);
}
}
public void writeRegisterModel(Register person){
String sqlStr=“insert into register values(?,?,?)”;
int ok=0;
try {
preSql=con.prepareStatement(sqlStr);
preSql.setString(1, person.getID());
preSql.setString(2, person.getPassword());
preSql.setString(3, person.getEmail());
ok=preSql.executeUpdate();
con.close();
}
catch (SQLException e){
JOptionPane.showMessageDialog(null,“id已被申请”,“警告”,JOptionPane.WARNING_MESSAGE);
}
if(ok!=0){
JOptionPane.showMessageDialog(null,“注册成功”,“恭喜”,JOptionPane.WARNING_MESSAGE);
}
}
}
model
package geng.model;
public class Login {
boolean loginSuccess = false;
String id;
String password;
public void setID(String id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public String getID(){
return id;
}
public String getPassword(){
return password;
}
public void setLoginSuccess(boolean bo){
loginSuccess=bo;
}
public boolean getLoginSuccess(){
return loginSuccess;
}
}
package geng.model;
public class Register {
String id;
String password;
String email;
public void setID(String id){
this.id=id;
}
public void setPassword(String password){
this.password=password;
}
public void setEmail(String email){
this.email=email;
}
public String getID(){
return id;
}
public String getPassword(){
return password;
}
public String getEmail(){
return email;
}
}
view
package geng.view;
import geng.handle.HandleLogin;
import geng.model.Login;
import geng.model.Register;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView extends JFrame implements ActionListener {
Login login;
JFrame jf;
JTextField inputID;
JPasswordField inputPassword;
JButton buttonLogin,NULL1,buttonRegister;
boolean loginSuccess;
Box baseBox,box1,box2;
LoginView(){
LoginView1();
}
void LoginView1(){
jf=new JFrame();
ImageIcon ccccc=new ImageIcon(“src/ccccc.jpg”);
JLabel label = new JLabel(ccccc);
label.setSize(ccccc.getIconWidth(),ccccc.getIconHeight());
jf.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
login=new Login();
inputID=new JTextField(12);
inputPassword=new JPasswordField(12);
NULL1=new JButton();
buttonLogin=new JButton(“登录”);
buttonRegister=new JButton(“注册”);
buttonRegister.addActionListener(this);
JLabel jLabel1=new JLabel(" WELCOME ");
jLabel1.setFont(new Font(“宋体”,1,50));
box1=Box.createVerticalBox();
box1.add(new JLabel(“ID:”));
box1.add(Box.createVerticalStrut(30));
box1.add(new JLabel(“密码:”));
box2=Box.createVerticalBox();
box2.add(Box.createVerticalStrut(30));
box2.add(inputID);
box2.add(Box.createVerticalStrut(25));
box2.add(inputPassword);
box2.add(Box.createVerticalStrut(8));
box2.add(buttonLogin);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(box2);
add(baseBox);
buttonLogin.addActionListener(this);
JPanel pan=(JPanel)jf.getContentPane();
pan.add(jLabel1);
pan.setOpaque(false);
pan.setLayout(new FlowLayout());
pan.add(baseBox);
pan.add(buttonRegister);
jf.setTitle(“登录窗口”);
jf.setSize(500,300);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public boolean isLoginSuccess() {
return loginSuccess;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonRegister) {
RegisterView registerView = new RegisterView();
registerView.setTitle("注册窗口");
}
if(e.getSource()==buttonLogin)
{
login.setID(inputID.getText());
char[] password = inputPassword.getPassword();
login.setPassword(new String(password));
HandleLogin handleLogin = new HandleLogin();
login = handleLogin.queryVerify(login);
loginSuccess = login.getLoginSuccess();
}
}
}
package geng.view;
import geng.handle.HandleLogin;
import geng.model.Register;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RegisterAndLoginView extends JFrame implements ActionListener {
RegisterView registerView;
LoginView loginView;
JButton buttonRegister;
public RegisterAndLoginView(){
loginView=new LoginView();
buttonRegister=new JButton(“注册”);
add(buttonRegister);
add(loginView);
buttonRegister.addActionListener(this);
}
public boolean isLoginSuccess(){
return loginView.isLoginSuccess();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==buttonRegister){
registerView=new RegisterView();
registerView.setTitle(“注册窗口”);
}
}
}
package geng.view;
import geng.handle.HandleRegister;
import geng.model.Register;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class RegisterView extends JFrame implements ActionListener {
Register register;
JTextField inputID,inputEmail;
JPasswordField inputPassword;
JButton buttonRegister;
Box baseBox,box1,box2;
public RegisterView(){
setLayout(new java.awt.FlowLayout());
RegisterView1();
}
void RegisterView1(){
JFrame jf=new JFrame();
ImageIcon ccccc=new ImageIcon(“src/ccccc.jpg”);
JLabel label = new JLabel(ccccc);
label.setSize(ccccc.getIconWidth(),ccccc.getIconHeight());
jf.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
register=new Register();
inputID=new JTextField(10);
inputPassword=new JPasswordField(10);
inputEmail=new JTextField(10);
buttonRegister=new JButton(“注册”);
box1=Box.createVerticalBox();
box1.add(new JLabel(" ID:"));
box1.add(Box.createVerticalStrut(8));
box1.add(new JLabel(" 密码:"));
box1.add(Box.createVerticalStrut(8));
box1.add(new JLabel(“你的电子邮箱:”));
box2=Box.createVerticalBox();
box2.add(inputID);
box2.add(Box.createVerticalStrut(8));
box2.add(inputPassword);
box2.add(Box.createVerticalStrut(8));
box2.add(inputEmail);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(2));
baseBox.add(box2);
add(baseBox);
add(buttonRegister);
buttonRegister.addActionListener(this);
JPanel pan=(JPanel)jf.getContentPane();
pan.setOpaque(false);
pan.setLayout(new FlowLayout());
pan.add(baseBox);
pan.add(buttonRegister);
jf.setTitle(“注册窗口”);
jf.setSize(400,200);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
register.setID(inputID.getText());
char[] pw=inputPassword.getPassword();
register.setPassword(new String(pw));
register.setEmail(inputEmail.getText());
HandleRegister handleRegister=new HandleRegister();
handleRegister.writeRegisterModel(register);
}
}
用户界面
package UserView;
import javax.swing.;
import javax.swing.table.DefaultTableModel;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Dingdanlisten implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame(“订单列表”);
frame.setSize( 500, 550);
frame.setLayout(new FlowLayout());
JTextArea textArea=new JTextArea(5,15);
JButton button;
button=new JButton(“付款”);
JButton button1=new JButton(“更新”);
button1.addActionListener(this);
if(e.getSource()button1){
String name;
String id;
String price;
String chubanDate;
String zhuangtai;
String[] database;
DefaultTableModel model;
JTable table;
String[] header = {“书名”, “出版号”, “价格”,“出版日期”,“付款状态”};
String[][] data = null;;
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con= FuView.GetDBConnection.connectDB(“book”,“root”,"");
if (connull)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery(“select * from bookList1”);
while (rs.next()) {
data = null;
name = rs.getString(“name”);
id = rs.getString(“ISBN”);
price = rs.getString(“price”);
chubanDate=rs.getString(“chubanDate”);
zhuangtai=rs.getString(“zhuangtai”);
database= new String[] { name, id, price,chubanDate,zhuangtai};
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{
System.out.println(a);
}
}
ActionListener listener2=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Connection con;
Statement sql;
ResultSet rs;
con = GetDBConnection.connectDB(“book”,“root”,"");
if(con==null) return;
try {
sql=con.createStatement();
String updateRecord=“update booklist1 set zhuangtai=‘已付款’ where zhuangtai=‘未付款’”;
int ok=sql.executeUpdate(updateRecord);
JOptionPane.showMessageDialog(null,“付款成功”,“恭喜”,JOptionPane.WARNING_MESSAGE);
con.close();
}
catch (SQLException c){
System.out.println(“记录中ISBN的值不能重复”+e);
}
}
};
button.addActionListener(listener2);
String name;
String id;
String price;
String chubanDate;
String zhuangtai;
String[] database;
DefaultTableModel model;
JTable table;
String[] header = {"书名", "出版号", "价格","出版日期","付款状态"};
String[][] data = null;;
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con= FuView.GetDBConnection.connectDB("book","root","");
if (con==null)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery("select * from bookList1");
while (rs.next()) {
data = null;
name = rs.getString("name");
id = rs.getString("ISBN");
price = rs.getString("price");
chubanDate=rs.getString("chubanDate");
zhuangtai=rs.getString("zhuangtai");
database= new String[] { name, id, price,chubanDate,zhuangtai};
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{System.out.println(a);}
frame.add(new JScrollPane(table));
frame.add(button);
frame.add(button1);
frame.setVisible(true);
}
}
package UserView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class GetDBConnection {
public static Connection connectDB(String DBName, String id, String password) {
try {
Class.forName(“com.mysql.cj.jdbc.Driver”);
}
catch (Exception e) {
System.out.println(“666666666666666666666”);
}
Connection con = null;
String uri =“jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC”;
try {
con = DriverManager.getConnection(uri,id,password);
}
catch (SQLException e) {
System.out.println(e);
}
return con;
}
}
package UserView;
import FuView.GetDBConnection;
import javax.swing.;
import javax.swing.table.DefaultTableModel;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Pinglunlisten extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame(“评论窗口”);
frame.setBounds( 500, 520,350,150);
frame.setLayout(new FlowLayout());
JButton button = new JButton(“评论”);
JTextArea textArea=new JTextArea(1,30);
frame.add(textArea);
frame.add(button);
frame.setVisible(true);
ActionListener listener =new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection con;
Statement sql;
ResultSet rs;
con= GetDBConnection.connectDB(“book”,“root”,"");
if(con==null) return;
String record=textArea.getText();
String addRecord =“insert into pinglun values(’”+record+"’)";
try {
sql=con.createStatement();
int ok= sql.executeUpdate(addRecord);
JOptionPane.showMessageDialog(null,“评论成功”,“恭喜”,JOptionPane.WARNING_MESSAGE);
con.close();
}
catch (SQLException c){
System.out.println(“记录中ISBN的值不能重复”+e);
}
}
};button.addActionListener(listener);
String name;
String[] database;
DefaultTableModel model;
JTable table;
String[] header = {"往期评论"};
String[][] data = null;
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con=GetDBConnection.connectDB("book","root","");
if (con==null)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery("select * from pinglun");
while (rs.next()) {
data = null;
name = rs.getString("pinglun");
database= new String[] { name };
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{System.out.println(a);}
frame.add(new JScrollPane(table));
frame.setBounds(500, 200, 500, 480);
frame.setVisible(true);
}
}
package UserView;
import javax.swing.;
import javax.swing.table.DefaultTableModel;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class UserListen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JFrame jf=new JFrame();
ImageIcon ccccc=new ImageIcon(“src/ccccc.jpg”);
JLabel label = new JLabel(ccccc);
label.setSize(ccccc.getIconWidth(),ccccc.getIconHeight());
jf.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
JButton button=new JButton(“购买”);
JButton button2=new JButton(“查阅图书”);
JLabel tishi=new JLabel(“请输入你要购买的图书:”);
JTextArea textArea=new JTextArea(1,15);
JTextArea textshow=new JTextArea(1,15);
JTextArea textArea1=new JTextArea(1,15);
JPanel pan=(JPanel)jf.getContentPane();
pan.add(tishi);
pan.setOpaque(false);
pan.setLayout(new FlowLayout());
pan.add(textArea);
pan.add(button);
pan.add(textArea1);
pan.add(button2);
pan.add(textshow);
String name;
String id;
String price;
String chubanDate;
String[] database;
DefaultTableModel model;
JTable table;
String[] header = {“书名”, “出版号”, “价格”,“出版日期”};
String[][] data = null;;
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con= FuView.GetDBConnection.connectDB(“book”,“root”,"");
if (connull)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery(“select * from bookList”);
while (rs.next()) {
data = null;
name = rs.getString(“name”);
id = rs.getString(“ISBN”);
price = rs.getString(“price”);
chubanDate=rs.getString(“chubanDate”);
database= new String[] { name, id, price,chubanDate };
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{System.out.println(a);}
jf.add(new JScrollPane(table));
jf.setBounds(500, 200, 500, 480);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
ActionListener listener=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Connection con;
Statement sql;
ResultSet rs;
con = GetDBConnection.connectDB(“book”,“root”,"");
if(connull) return;
String record=textArea.getText();
String addRecord=“INSERT INTO booklist1 SELECT *FROM booklist WHERE NAME= '” + record + " ‘";
try {
sql=con.createStatement();
int ok= sql.executeUpdate(addRecord);
String updateRecord="update booklist1 set zhuangtai=‘未付款’ where name=’"+ record +"’";
ok=sql.executeUpdate(updateRecord);
JOptionPane.showMessageDialog(null,“购买成功”,“恭喜”,JOptionPane.WARNING_MESSAGE);
con.close();
}
catch (SQLException c){
System.out.println(“记录中ISBN的值不能重复”+e);
}
}
};
button.addActionListener(listener);
ActionListener listener1=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Connection con;
Statement sql;
ResultSet rs;
con = FuView.GetDBConnection.connectDB("book","root","");
if(con==null) return;
String record=textArea1.getText();
try {
sql=con.createStatement();
rs=sql.executeQuery("select * from booklist where name= '" + record+"'");
while(rs.next()){
String ISBN= rs.getString(1);
String name= rs.getString(2);
String price= rs.getString(3);
String date= rs.getString(4);
textshow.append( ISBN+","+ name+"," +price+","+ date+"\n");
}
JOptionPane.showMessageDialog(null,"查找成功","恭喜",JOptionPane.WARNING_MESSAGE);
con.close();
}
catch (Exception c){
JOptionPane.showMessageDialog(null,"查找失败","错误",JOptionPane.WARNING_MESSAGE);
c.printStackTrace();
}
}
};
button2.addActionListener(listener1);
}
}
package UserView;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class WindowUser extends JFrame {
public WindowUser(){
init();
}
void init(){
JFrame jf=new JFrame();
ImageIcon ccccc=new ImageIcon("src/ccccc.jpg");
JLabel label = new JLabel(ccccc);
label.setSize(ccccc.getIconWidth(),ccccc.getIconHeight());
jf.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
JLabel jLabel1=new JLabel(" WELCOME ");
jLabel1.setFont(new Font("宋体",1,50));
JButton button=new JButton("我的订单");
JButton button2=new JButton("选购图书");
JButton button1=new JButton("评论");
Pinglunlisten listener3;
UserListen listener;
Dingdanlisten listener2;
listener=new UserListen();
listener2=new Dingdanlisten();
listener3=new Pinglunlisten();
JPanel pan=(JPanel)jf.getContentPane();
pan.setOpaque(false);
pan.setLayout(new FlowLayout());
button.addActionListener(listener2);
button2.addActionListener(listener);
button1.addActionListener(listener3);
jf.setTitle("用户界面");
pan.add(jLabel1);
pan.add(button);
pan.add(button2);
pan.add(button1);
jf.setSize(450,320);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
}
管理员界面
package FuView;
import javax.swing.;
import javax.swing.table.DefaultTableModel;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Blisten implements ActionListener{
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame(“增加图书”);
frame.setBounds( 500, 520,350,150);
frame.setLayout(new FlowLayout());
JButton button = new JButton(“增加图书”);
JLabel tishi=new JLabel(“请输入图书的信息(格式必须包含序列号书名价格出版日期)”);
JTextArea textArea=new JTextArea(1,30);
frame.add(tishi);
frame.add(textArea);
frame.add(button);
frame.setVisible(true);
ActionListener listener =new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection con;
Statement sql;
ResultSet rs;
con= GetDBConnection.connectDB(“book”,“root”,"");
if(connull) return;
String record=textArea.getText();
String addRecord =“insert into booklist values(”+record+")";
try {
sql=con.createStatement();
int ok= sql.executeUpdate(addRecord);
JOptionPane.showMessageDialog(null,“增加成功”,“恭喜”,JOptionPane.WARNING_MESSAGE);
con.close();
}
catch (SQLException c){
System.out.println(“记录中ISBN的值不能重复”+e);
}
}
};
button.addActionListener(listener);
String name;
String id;
String price;
String chubanDate;
String[] database;
DefaultTableModel model;
JTable table;
String[] header = {“书名”, “出版号”, “价格”,“出版日期”};
String[][] data = null;
model = new DefaultTableModel(data,header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con=GetDBConnection.connectDB(“book”,“root”,"");
if (connull)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery(“select * from bookList”);
while (rs.next()) {
data = null;
name = rs.getString(“name”);
id = rs.getString(“ISBN”);
price = rs.getString(“price”);
chubanDate=rs.getString(“chubanDate”);
database= new String[] { name, id, price,chubanDate };
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{System.out.println(a);}
frame.add(new JScrollPane(table));
frame.setBounds(500, 200, 500, 480);
frame.setVisible(true);
}
}
package FuView;
import javax.swing.;
import javax.swing.table.DefaultTableModel;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Clisten implements ActionListener{
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame(“删除图书”);
frame.setBounds( 500, 520,300,150);
frame.setLayout(new FlowLayout());
JButton button = new JButton(“删除图书”);
JLabel tishi=new JLabel(“请输入要删除的书名:”);
JTextArea textArea=new JTextArea(1,15);
frame.add(tishi);
frame.add(textArea);
frame.add(button);
frame.setVisible(true);
ActionListener listener =new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection con;
Statement sql;
ResultSet rs;
con = GetDBConnection.connectDB(“book”,“root”,"");
if(con==null) return;
String record=textArea.getText();
String updateRecord=“delete from booklist where name= '” + record+"’";
try {
sql=con.createStatement();
int ok= sql.executeUpdate(updateRecord);
rs=sql.executeQuery("select * from booklist");
while(rs.next()){
String ISBN= rs.getString(1);
String name= rs.getString(2);
String price= rs.getString(3);
String date= rs.getString(4);
System.out.printf("%s,%s,%s,%s.\n", ISBN,name,price,date);
}
JOptionPane.showMessageDialog(null,"删除成功","恭喜",JOptionPane.WARNING_MESSAGE);
con.close();
}
catch (Exception c){
c.printStackTrace();
}
}
};
button.addActionListener(listener);
String name;
String id;
String price;
String chubanDate;
String[] database;
JFrame frame1;
DefaultTableModel model;
JTable table;
String[] header = {"书名", "出版号", "价格","出版日期"};
String[][] data = null;
frame1 = new JFrame("书库");
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con=GetDBConnection.connectDB("book","root","");
if (con==null)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery("select * from bookList");
while (rs.next()) {
data = null;
name = rs.getString("name");
id = rs.getString("ISBN");
price = rs.getString("price");
chubanDate=rs.getString("chubanDate");
database= new String[] { name, id, price,chubanDate };
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{System.out.println(a);}
frame.add(new JScrollPane(table));
frame.setBounds(500, 200, 500, 480);
frame.setVisible(true);
}
}
package FuView;
import java.sql.*;
public class GetDBConnection {
public static Connection connectDB(String DBName,String id,String password) {
try {
Class.forName(“com.mysql.cj.jdbc.Driver”);
}
catch (Exception e) {
System.out.println(“666666666666666666666”);
}
Connection con = null;
String uri =“jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC”;
try {
con = DriverManager.getConnection(uri,id,password);
}
catch (SQLException e) {
System.out.println(e);
}
return con;
}
}
package FuView;
import javax.swing.;
import javax.swing.table.DefaultTableModel;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Serachlisten implements ActionListener{
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame(“搜索图书”);
JTextArea textshow=new JTextArea(1,10);
frame.setBounds( 500, 300,500,450);
frame.setLayout(new FlowLayout());
JButton button = new JButton(“搜索图书”);
JLabel tishi=new JLabel(“请输入要搜索的书名:”);
JLabel tishi2=new JLabel(“结果:”);
JTextArea textArea=new JTextArea(1,10);
frame.add(tishi);
frame.add(textArea);
frame.add(button);
frame.setVisible(true);
frame.add(tishi2);
frame.add(textshow);
ActionListener listener=new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection con;
Statement sql;
ResultSet rs;
con =GetDBConnection.connectDB(“book”,“root”,"");
if(con==null) return;
String record=textArea.getText();
try {
sql=con.createStatement();
rs=sql.executeQuery(“select * from booklist where name= '” + record+"’");
while(rs.next()){
String ISBN= rs.getString(1);
String name= rs.getString(2);
String price= rs.getString(3);
String date= rs.getString(4);
textshow.append( ISBN+","+ name+"," +price+","+ date+"\n");
}
JOptionPane.showMessageDialog(null,“查询成功”,“恭喜”,JOptionPane.WARNING_MESSAGE);
con.close();
}
catch (Exception c){
c.printStackTrace();
}
}
};
button.addActionListener(listener);
String name;
String id;
String price;
String chubanDate;
String[] database;
DefaultTableModel model;
JTable table;
String[] header = {"书名", "出版号", "价格","出版日期"};
String[][] data = null;
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con=GetDBConnection.connectDB("book","root","");
if (con==null)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery("select * from bookList");
while (rs.next()) {
data = null;
name = rs.getString("name");
id = rs.getString("ISBN");
price = rs.getString("price");
chubanDate=rs.getString("chubanDate");
database= new String[] { name, id, price,chubanDate };
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{System.out.println(a);}
frame.add(new JScrollPane(table));
frame.setBounds(500, 200, 500, 480);
frame.setVisible(true);
}
}
package FuView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class UserConnection {
public static Connection connectDBC(String DBCName, String id, String password) {
try {
Class.forName(“com.mysql.cj.jdbc.Driver”);
}
catch (Exception e) {
System.out.println(“666666666666666666666”);
}
Connection con = null;
String uri =“jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC”;
try {
con = DriverManager.getConnection(uri,id,password);
}
catch (SQLException e) {
System.out.println(e);
}
return con;
}
}
package FuView;
import javax.swing.;
import javax.swing.table.DefaultTableModel;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Windowapp extends JFrame implements ActionListener {
public JButton button1 ,button2,button4,button5,button6,button7;
Box box1;
public Windowapp(){
init();
}
void init(){
JFrame jf=new JFrame();
ImageIcon ccccc=new ImageIcon(“src/ccccc.jpg”);
JLabel label = new JLabel(ccccc);
label.setSize(ccccc.getIconWidth(),ccccc.getIconHeight());
jf.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
box1=Box.createVerticalBox();
JLabel jLabel1=new JLabel(" WELCOME “);
jLabel1.setFont(new Font(“宋体”,1,50));
JLabel jLabel2=new JLabel(” “);
jLabel1.setFont(new Font(“宋体”,1,50));
JLabel jLabel3=new JLabel(” 图书库存 “);
jLabel3.setFont(new Font(“宋体”,1,36));
button1=new JButton(“增加图书”);
button2=new JButton(“删除图书”);
button4=new JButton(“搜索图书”);
button5=new JButton(“用户信息”);
button6=new JButton(“订单系统”);
button7=new JButton(“用户评论区”);
Blisten listener2;
Clisten listener3;
Serachlisten listener4;
listener2=new Blisten();
listener3=new Clisten();
listener4=new Serachlisten();
button1.addActionListener(listener2);
button2.addActionListener(listener3);
button4.addActionListener(listener4);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
JPanel pan=(JPanel)jf.getContentPane();
pan.add(jLabel1);
jf.add(button1);
jf.add(button2);
jf.add(button4);
jf.add(button5);
jf.add(button6);
jf.add(button7);
jf.add(jLabel2);
jf.add(jLabel3);
pan.setOpaque(false);
pan.setLayout(new FlowLayout());
String name;
String id;
String price;
String chubanDate;
String[] database;
DefaultTableModel model;
JTable table;
String[] header = {“书名”, “出版号”, “价格”,“出版日期”};
String[][] data = null;
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con=GetDBConnection.connectDB(“book”,“root”,”");
if (con==null)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery(“select * from bookList”);
while (rs.next()) {
data = null;
name = rs.getString(“name”);
id = rs.getString(“ISBN”);
price = rs.getString(“price”);
chubanDate=rs.getString(“chubanDate”);
database= new String[] { name, id, price,chubanDate };
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{System.out.println(a);}
jf.add(new JScrollPane(table));
jf.setBounds(500, 400, 600, 620);
jf.setVisible(true);
jf.setTitle(“管理员界面”);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button5){
String name;
String id;
String price;
String[] database;
JFrame frame;
DefaultTableModel model;
JTable table;
String[] header = {"账号", "密码", "邮箱"};
String[][] data = null;
frame = new JFrame("用户系统");
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con=UserConnection.connectDBC("user","root","");
if (con==null)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery("select * from register");
while (rs.next()) {
data = null;
name = rs.getString("id");
id = rs.getString("password");
price = rs.getString("email");
database= new String[] { name, id, price};
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{System.out.println(a);}
frame.add(new JScrollPane(table));
frame.setBounds(500, 200, 500, 480);
frame.setVisible(true);
}
if(e.getSource()==button6){
JFrame frame = new JFrame("订单列表");
frame.setSize( 500, 300);
String name;
String id;
String price;
String chubanDate;
String zhuangtai;
String[] database;
DefaultTableModel model;
JTable table;
String[] header = {"书名", "出版号", "价格","出版日期","付款状态"};
String[][] data = null;;
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con= FuView.GetDBConnection.connectDB("book","root","");
if (con==null)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery("select * from bookList1");
while (rs.next()) {
data = null;
name = rs.getString("name");
id = rs.getString("ISBN");
price = rs.getString("price");
chubanDate=rs.getString("chubanDate");
zhuangtai=rs.getString("zhuangtai");
database= new String[] { name, id, price,chubanDate,zhuangtai};
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{System.out.println(a);}
frame.add(new JScrollPane(table));
frame.setVisible(true);
}
if (e.getSource()==button7){
JFrame frame = new JFrame("客户评论区");
frame.setSize( 500, 300);
String name;
String[] database;
DefaultTableModel model;
JTable table;
String[] header = {"往期评论"};
String[][] data = null;
model = new DefaultTableModel(data, header);
table = new JTable(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Connection con;
con=GetDBConnection.connectDB("book","root","");
if (con==null)
return;
Statement sql;
ResultSet rs;
try {
sql=con.createStatement();
rs= sql.executeQuery("select * from pinglun");
while (rs.next()) {
data = null;
name = rs.getString("pinglun");
database= new String[] { name };
model.addRow(database);
}
con.close();
}
catch (SQLException a)
{
System.out.println(a);
}
frame.add(new JScrollPane(table));
frame.setBounds(500, 200, 500, 480);
frame.setVisible(true);
}
}
}