java连接数据库请转另一篇博客
https://blog.csdn.net/pig_boss/article/details/109731567
1.主界面类(Main_InterFace.java):
package Student_SQLserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main_InterFace extends JFrame {
public Main_InterFace() {
setSize(550, 350);
setLocation(500, 200);
setResizable(false);
setTitle("图书管理");
setDefaultCloseOperation(3);
setLayout(new FlowLayout());
JPanel jPanel = new JPanel();
JLabel jlTitle = new JLabel("图书管理器", JLabel.CENTER);
jlTitle.setFont(new Font(null, 3, 30));
jPanel.setLayout(new GridLayout(2, 2, 10, 20));
//button.setFocusPainted(false);
JButton btnInsert = new JButton("添加书籍");
JButton btnDelete = new JButton("删除书籍");
JButton btnSelect = new JButton("查找书籍");
JButton btnUpdate = new JButton("修改书籍");
//按钮点击时是否绘制焦点,默认为true ,这里设置为false
btnInsert.setFocusPainted(false);
btnDelete.setFocusPainted(false);
btnSelect.setFocusPainted(false);
btnUpdate.setFocusPainted(false);
Font font = new Font(null, 1, 20);
Dimension dimen = new Dimension(200, 60);
btnInsert.setPreferredSize(dimen);
btnDelete.setPreferredSize(dimen);
btnSelect.setPreferredSize(dimen);
btnUpdate.setPreferredSize(dimen);
btnInsert.setFont(font);
btnDelete.setFont(font);
btnSelect.setFont(font);
btnUpdate.setFont(font);
jPanel.add(btnInsert);
jPanel.add(btnDelete);
jPanel.add(btnSelect);
jPanel.add(btnUpdate);
add(jlTitle);
add(jPanel);
//为按钮添加监听事件
btnInsert.addActionListener(new MainInsert());
btnDelete.addActionListener(new MainDelete());
btnSelect.addActionListener(new MainSelect());
btnUpdate.addActionListener(new MainUpdate());
setVisible(true);
}
public static void main(String[] args) {
new Main_InterFace();
}
//主界面——查询按钮监听事件
private class MainSelect implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
new Select();
}
}
//主界面——删除按钮监听事件
private class MainDelete implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
new Delete();
}
}
//主界面——插入按钮监听事件
private class MainInsert implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
new Insert();
}
}
//主界面——修改按钮监听事件
private class MainUpdate implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
new Update();
}
}
}
2.连接数据库方法类(ToSQLserver.java)
package Student_SQLserver;
import java.sql.*;
import java.util.ArrayList;
public class ToSQLserver {
public static Connection dbConn = null;
public PreparedStatement statement = null;
public ResultSet result = null;
//这里除了一个错,ArrayList 不能只声明而不实例化,不然会发生空指针异常。
public ArrayList<String> bookInfo = new ArrayList<>();
public int success = 0;
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=booksManager";
String name = "sa";
String passwd = "wry123";
String toSelect = "select * from [BookInFo] where Bname=";
public ToSQLserver() {
try {
//1.加载驱动
//Class.forName方法的作用,就是初始化给定的类.而我们给定的MySQL的Driver类中,
// 它在静态代码块中通过JDBC的DriverManager注册了一下驱动.我们也可以直接使用JDBC的驱动管理器注册mysql驱动.
// 从而代替使用Class.forName.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.连接
dbConn = DriverManager.getConnection(dbURL, name, passwd);
System.out.println("连接数据库成功");
} catch (Exception e) {
e.printStackTrace();
}
}
//数据库查询方法,返回值为 ArrayList
public ArrayList<String> selectmethod(String text1) {
String uninSelect = toSelect + "'" + text1 + "'";
System.out.println(uninSelect);
try {
statement = dbConn.prepareStatement(uninSelect);
result = statement.executeQuery();
System.out.println("查询成功");
while (result.next()) {
bookInfo.add(result.getString("Bno"));
bookInfo.add(result.getString("Bname"));
bookInfo.add(result.getString("Bauthor"));
bookInfo.add(result.getString("Bclass"));
}
} catch (Exception e) {
e.printStackTrace();
}
return bookInfo;
}
//插入数据方法
public void insertmethod(ArrayList list) {
//insert BookInfo(Bno,Bname,Bauthor,Bclass)values('0003','西游记','吴承恩','神话类')
String toInsert = "insert BookInfo(Bno,Bname,Bauthor,Bclass)values";
toInsert = toInsert + "('" + list.get(0) + "','" + list.get(1) + "','" + list.get(2)
+ "','" + list.get(3) + "')";
try {
statement = dbConn.prepareStatement(toInsert);
success = statement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
success = 0;
}
}
//修改数据方法
public void updatemethod(ArrayList list) {
//update BookInFo set Bname='水浒传',Bauthor='施耐庵',Bclass='历史类' where Bno='0004'
String toUpdate = "update BookInFo set Bname=";
toUpdate = toUpdate + "'" + list.get(1) + "',Bauthor='" + list.get(2) + "',Bclass='"
+ list.get(3) + "' where Bno='" + list.get(0) + "'";
System.out.println(toUpdate);
try {
statement = dbConn.prepareStatement(toUpdate);
success = statement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
success = 0;
}
}
//删除数据方法
public void deletemethod(ArrayList list){
//delete from BookInfo where Bname='西游记'
String toDelete="delete from BookInfo where Bname=";
toDelete=toDelete+"'"+list.get(1)+"'";
try {
statement = dbConn.prepareStatement(toDelete);
success = statement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
success = 0;
}
}
}
3.查询界面类(Select.java):
package Student_SQLserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Select extends JFrame {
//便于可以在按钮事件 SelectAction 内部类中访问
private final JPanel jp;
private final JTextField bname;
//用数组接收查询结果,方便管理数据
public ArrayList<String> sellist = new ArrayList<>();
JLabel jlToast = new JLabel("", JLabel.CENTER);
//将文本内容对象放到构造函数外面,便于可以在按钮事件 SelectAction 内部类中访问
JTextArea textBno1 = new JTextArea();
JTextArea textVname1 = new JTextArea();
JTextArea textBauthor1 = new JTextArea();
JTextArea textBclass1 = new JTextArea();
public Select() {
setSize(550, 350);
setLocation(500, 200);
setResizable(false);
setTitle("查询书籍");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
jp = new JPanel();//查询面板
JPanel jpText = new JPanel();//查询结果面板
JPanel jpToast = new JPanel();//查询是否成功面板
jp.setLayout(new FlowLayout());//将JFrame设置为流式布局
jpText.setLayout(new GridLayout(2, 4, 10, 10));
JLabel textBno = new JLabel("书号", JLabel.CENTER);
JLabel textBname = new JLabel("书名", JLabel.CENTER);
JLabel textBauthor = new JLabel("作者", JLabel.CENTER);
JLabel textBclass = new JLabel("书类", JLabel.CENTER);
Dimension dimen = new Dimension(120, 30);//构造一个大小对象
textBno.setPreferredSize(dimen);
textBname.setPreferredSize(dimen);
textBauthor.setPreferredSize(dimen);
textBclass.setPreferredSize(dimen);
textBno1.setPreferredSize(dimen);
textVname1.setPreferredSize(dimen);
textBauthor1.setPreferredSize(dimen);
textBclass1.setPreferredSize(dimen);
jlToast.setPreferredSize(new Dimension(200, 40));//设置提示框大小
jlToast.setFont(new Font(null, 1, 20));//设置提示文本字体
jlToast.setForeground(Color.RED);//设置字体颜色
JLabel toast = new JLabel("书名:");//文本框
bname = new JTextField();
JButton select = new JButton("查询");
//设置编辑框的大小
bname.setPreferredSize(new Dimension(250, 30));
//按钮添加事件
SelectAction selectAction = new SelectAction();
select.addActionListener(selectAction);
//输入查询组件
jp.add(toast);
jp.add(bname);
jp.add(select);
//查询结果显示组件
jpText.add(textBno);
jpText.add(textBname);
jpText.add(textBauthor);
jpText.add(textBclass);
jpText.add(textBno1);
jpText.add(textVname1);
jpText.add(textBauthor1);
jpText.add(textBclass1);
//查询是否成功组件
jpToast.add(jlToast);
//因为这个类是直接继承自JFrame,所以直接调用JFrame中的方法
add(jp);
add(jpText);
add(jpToast);
setVisible(true);//这段代码一般放在最后,设置窗口是否可见,默认是false
}
//设置按钮监听类,继承自 ActionListener 接口
private class SelectAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String bookName = bname.getText();
ToSQLserver conSQL = new ToSQLserver();//实例化数据库查询类
sellist = conSQL.selectmethod(bookName);
System.out.println(sellist);
if (sellist.size() != 0) {
jlToast.setText("查询成功");
textBno1.setText(sellist.get(0));
textVname1.setText(sellist.get(1));
textBauthor1.setText(sellist.get(2));
textBclass1.setText(sellist.get(3));
} else {
jlToast.setText("抱歉,没有这本书");
textBno1.setText(null);
textVname1.setText(null);
textBauthor1.setText(null);
textBclass1.setText(null);
}
//将查询得到的数据添加到 JTextArea 对象上
jp.updateUI();//更新界面显示查询结果
}
}
}
4.添加界面(Insert.java):
package Student_SQLserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Insert extends JFrame {
public JPanel jpText;
public ArrayList<String> inlist = new ArrayList<>();
JTextField textBno1 = new JTextField();
JTextField textBname1 = new JTextField();
JTextField textBauthor1 = new JTextField();
JTextField textBclass1 = new JTextField();
JLabel jlToast = new JLabel("", JLabel.CENTER);
public Insert() {
setSize(550, 350);
setLocation(500, 200);
setResizable(false);
setTitle("添加书籍");
setDefaultCloseOperation(3);
setLayout(new FlowLayout());
jpText = new JPanel();
JPanel jp = new JPanel();
JButton btnInsert = new JButton("添加");
JLabel textBno = new JLabel("书号", JLabel.CENTER);
JLabel textBname = new JLabel("书名", JLabel.CENTER);
JLabel textBauthor = new JLabel("作者", JLabel.CENTER);
JLabel textBclass = new JLabel("书类", JLabel.CENTER);
jpText.setLayout(new GridLayout(2, 4, 10, 10));
jp.setLayout(new GridLayout(2, 1, 0, 20));
btnInsert.setFont(new Font(null, 1, 20));
btnInsert.addActionListener(new InsettAction());
jlToast.setForeground(Color.RED);
jlToast.setFont(new Font(null, 1, 20));//设置提示文本字体
jlToast.setPreferredSize(new Dimension(200, 40));//设置提示框大小
Dimension dimen = new Dimension(120, 30);
textBno.setPreferredSize(dimen);
textBname.setPreferredSize(dimen);
textBauthor.setPreferredSize(dimen);
textBclass.setPreferredSize(dimen);
textBno1.setPreferredSize(dimen);
textBname1.setPreferredSize(dimen);
textBauthor1.setPreferredSize(dimen);
textBclass1.setPreferredSize(dimen);
//添加书籍的数据加到面板中
jpText.add(textBno);
jpText.add(textBname);
jpText.add(textBauthor);
jpText.add(textBclass);
jpText.add(textBno1);
jpText.add(textBname1);
jpText.add(textBauthor1);
jpText.add(textBclass1);
//按钮和提示框加入到一个面板中
jp.add(btnInsert);
jp.add(jlToast);
add(jpText);
add(new Label("----------------------------------------------------------------" +
"--------------------------------------------------------------", Label.CENTER));
add(jp);
setVisible(true);
}
public class InsettAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
inlist.add(textBno1.getText());
inlist.add(textBname1.getText());
inlist.add(textBauthor1.getText());
inlist.add(textBclass1.getText());
System.out.println(inlist);
ToSQLserver conSQL = new ToSQLserver();
conSQL.insertmethod(inlist);
if (conSQL.success == 1) {
jlToast.setText("添加成功");
} else {
jlToast.setText("添加失败");
}
}
}
}
5.修改界面(Update.java):
package Student_SQLserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Update extends JFrame {
public JPanel jpText;
public ArrayList<String> inlist = new ArrayList<>();
JTextField textBno1 = new JTextField();
JTextField textBname1 = new JTextField();
JTextField textBauthor1 = new JTextField();
JTextField textBclass1 = new JTextField();
JLabel jlToast = new JLabel("", JLabel.CENTER);
public Update() {
setSize(550, 350);
setLocation(500, 200);
setResizable(false);
setTitle("修改书籍");
setDefaultCloseOperation(3);
setLayout(new FlowLayout());
jpText = new JPanel();
JPanel jp = new JPanel();
JButton btnInsert = new JButton("更新");
JLabel textBno = new JLabel("书号", JLabel.CENTER);
JLabel textBname = new JLabel("书名", JLabel.CENTER);
JLabel textBauthor = new JLabel("作者", JLabel.CENTER);
JLabel textBclass = new JLabel("书类", JLabel.CENTER);
jpText.setLayout(new GridLayout(2, 4, 10, 10));
jp.setLayout(new GridLayout(2, 1, 0, 20));
btnInsert.setFont(new Font(null, 1, 20));
btnInsert.addActionListener(new UpdateAction());
jlToast.setForeground(Color.RED);
jlToast.setFont(new Font(null, 1, 20));//设置提示文本字体
jlToast.setPreferredSize(new Dimension(200, 40));//设置提示框大小
Dimension dimen = new Dimension(120, 30);
textBno.setPreferredSize(dimen);
textBname.setPreferredSize(dimen);
textBauthor.setPreferredSize(dimen);
textBclass.setPreferredSize(dimen);
textBno1.setPreferredSize(dimen);
textBname1.setPreferredSize(dimen);
textBauthor1.setPreferredSize(dimen);
textBclass1.setPreferredSize(dimen);
//添加书籍的数据加到面板中
jpText.add(textBno);
jpText.add(textBname);
jpText.add(textBauthor);
jpText.add(textBclass);
jpText.add(textBno1);
jpText.add(textBname1);
jpText.add(textBauthor1);
jpText.add(textBclass1);
//按钮和提示框加入到一个面板中
jp.add(btnInsert);
jp.add(jlToast);
add(jpText);
add(new Label("----------------------------------------------------------------" +
"--------------------------------------------------------------", Label.CENTER));
add(jp);
setVisible(true);
}
public class UpdateAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
inlist.add(textBno1.getText());
inlist.add(textBname1.getText());
inlist.add(textBauthor1.getText());
inlist.add(textBclass1.getText());
System.out.println(inlist);
ToSQLserver conSQL = new ToSQLserver();
conSQL.updatemethod(inlist);
if (conSQL.success == 1) {
jlToast.setText("更新成功");
} else {
jlToast.setText("更新失败");
}
}
}
}
6.删除界面(Delete.java):
package Student_SQLserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Delete extends JFrame {
public JPanel jpText;
public ArrayList<String> inlist = new ArrayList<>();
JTextField textBno1 = new JTextField();
JTextField textBname1 = new JTextField();
JTextField textBauthor1 = new JTextField();
JTextField textBclass1 = new JTextField();
JLabel jlToast = new JLabel("", JLabel.CENTER);
public Delete() {
setSize(550, 350);
setLocation(500, 200);
setResizable(false);
setTitle("删除书籍");
setDefaultCloseOperation(3);
setLayout(new FlowLayout());
jpText = new JPanel();
JPanel jp = new JPanel();
JButton btnInsert = new JButton("删除");
JLabel textBno = new JLabel("书号", JLabel.CENTER);
JLabel textBname = new JLabel("书名", JLabel.CENTER);
JLabel textBauthor = new JLabel("作者", JLabel.CENTER);
JLabel textBclass = new JLabel("书类", JLabel.CENTER);
jpText.setLayout(new GridLayout(2, 4, 10, 10));
jp.setLayout(new GridLayout(2, 1, 0, 20));
btnInsert.setFont(new Font(null, 1, 20));
btnInsert.addActionListener(new DeleteAction());
jlToast.setForeground(Color.RED);
jlToast.setFont(new Font(null, 1, 20));//设置提示文本字体
jlToast.setPreferredSize(new Dimension(200, 40));//设置提示框大小
Dimension dimen = new Dimension(120, 30);
textBno.setPreferredSize(dimen);
textBname.setPreferredSize(dimen);
textBauthor.setPreferredSize(dimen);
textBclass.setPreferredSize(dimen);
textBno1.setPreferredSize(dimen);
textBname1.setPreferredSize(dimen);
textBauthor1.setPreferredSize(dimen);
textBclass1.setPreferredSize(dimen);
//添加书籍的数据加到面板中
jpText.add(textBno);
jpText.add(textBname);
jpText.add(textBauthor);
jpText.add(textBclass);
jpText.add(textBno1);
jpText.add(textBname1);
jpText.add(textBauthor1);
jpText.add(textBclass1);
//按钮和提示框加入到一个面板中
jp.add(btnInsert);
jp.add(jlToast);
add(jpText);
add(new Label("----------------------------------------------------------------" +
"--------------------------------------------------------------", Label.CENTER));
add(jp);
setVisible(true);
}
public class DeleteAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
inlist.add(textBno1.getText());
inlist.add(textBname1.getText());
inlist.add(textBauthor1.getText());
inlist.add(textBclass1.getText());
System.out.println(inlist);
ToSQLserver conSQL = new ToSQLserver();
conSQL.deletemethod(inlist);
if (conSQL.success == 1) {
jlToast.setText("删除成功");
} else {
jlToast.setText("没有这本书");
}
}
}
}
效果图:
ps:时间是个常数,但也是个变数。勤奋的人无穷多,懒惰的人无穷少。