個人分析
java擷取資料庫連結,首先需要一個jar包。
都是一些資料庫的增删改查,很簡單。
資料庫,表結構如下:

代碼如下:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener {
private static String[] arr = { "微笑", "傷心", "自嘲", "大笑", "生無可戀" };// 這裡存放的是表情,等下放進那個JComboBox
private static Font ff = new Font("宋體", Font.PLAIN, 25);// 這裡的字型是設定的所有控件上的
private static JPanel top, botton, right;// 分别對應上中下右四個面闆
private static JScrollPane middle;// 因為中間是文本域,需要滾動條,這裡就幹脆用滾動條面闆
private static JLabel msbord;// 留言闆
private static JTextArea msbordcon;// 留言闆内容
private static JLabel you;// 你
private static JComboBox expresion;// 表情下拉框
private static JLabel yousay;// 你說标簽
private static JTextField words;// 所留言内容的輸入框
private static JButton submit;// 送出按鈕
private static JButton clearPrint;// 清屏按鈕
private static JButton ttop;// 置頂按鈕
private static JButton end;// 置尾按鈕
public static void main(String[] args) throws Exception {
Test t = new Test();
}
public Test() throws Exception {
this.add(top, BorderLayout.NORTH);// 添加最上方的面闆
this.add(middle);// 同理,這裡也是,不指定布局,就是邊框布局,并且預設位置居中
this.add(botton, BorderLayout.SOUTH);// 下方的JPanel,标準說法,就是南方
this.add(right, BorderLayout.EAST);
this.setSize(1218, 800);
this.setLocation(300, 100);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pri();// 這裡,第一次将軟體打開的時候,就會去讀取資料庫資料
}
{// 初始化控件
msbord = new JLabel("留言闆");
msbord.setFont(ff);
top = new JPanel(new FlowLayout());// 最上面給的留言闆,這裡用的流式布局
top.add(msbord);// 添加留言闆标簽,并且居中,
msbordcon = new JTextArea("留言内容:\n");// 為了美觀 \n
msbordcon.setEditable(false);// 設定文本框預設不可編輯
msbordcon.setFont(ff);
middle = new JScrollPane();
middle.setViewportView(msbordcon);// 這裡将JScrollpane需要展示的控件放進去//
you = new JLabel("你 ");
you.setFont(ff);
expresion = new JComboBox(arr);// 這裡放的就是上面那個表情數組
expresion.setFont(ff);
yousay = new JLabel("着是說: ");
yousay.setFont(ff);
words = new JTextField(25);// 這裡的輸入框預設長度
words.setFont(ff);
submit = new JButton("送出");
submit.addActionListener(this);
submit.setFont(ff);
botton = new JPanel(new FlowLayout());// 這裡采用流式布局,居中
botton.add(you);
botton.add(expresion);
botton.add(yousay);
botton.add(words);
botton.add(submit);// 添加完畢
clearPrint = new JButton("清屏");
clearPrint.addActionListener(this);
clearPrint.setFont(ff);
ttop = new JButton("置頂");
ttop.addActionListener(this);
ttop.setFont(ff);
end = new JButton("置尾");
end.setFont(ff);
right = new JPanel(new GridLayout(9, 1, 5, 50));// 最右邊的三個按鈕就采用網格布局.3*1
right.add(clearPrint);
right.add(ttop);
right.add(end);
}
int i = 0;
public void actionPerformed(ActionEvent e) {// 按鈕事件處理類
i++;
if (e.getActionCommand().equals("送出")) {
String expre = (String) expresion.getSelectedItem();// 拿到表情
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");// 設定時間格式
String date = sdf.format(new Date());// 拿到目前時間,并格式好了
String content = words.getText();// 拿到内容
words.setText(null);// 内容重置為null
cord c = new cord(1, date, expre, content);// 取到資料,并封裝成cord對象,調用addcord方法存入資料庫
// 注意一點,這裡的第一項,也就是id。隻有占位作用
try {
addcord(c);
pri();
} catch (Exception e1) {
}
} else if (e.getActionCommand().equals("清屏")) {
msbordcon.setText("留言内容:\n");
}
}
public static ArrayList<cord> getSource() throws Exception {// 查詢資料庫所有資料,給resultset數組,并将結果return
Class.forName("com.mysql.jdbc.Driver");// 利用類加載器,加載資料庫驅動類
String url = "jdbc:mysql://localhost:3306/test";// 這裡的test是你資料庫的名字
Connection connection = connection = DriverManager.getConnection(url,
"A", "B");
// 拿到連接配接,這裡的倆個引号AB,分别填你的資料庫使用者名與密碼
String sql = "select * from recored where 3=?";// 編寫sql查詢語句,這裡因為是普通作業,就直接全部查完
// 個人習慣,這裡就用預編譯的方式查詢
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 3);// 第一個問号,指派為3,
ResultSet rs = preparedStatement.executeQuery();// 拿到結果集
ArrayList<cord> cords = new ArrayList();
while (rs.next()) {// 如果結果集還有下一個資料
// 将資料封裝成對象,再存入List集合
cords.add(new cord(rs.getInt("id"), rs.getString("data"), rs
.getString("expression"), rs.getString("content")));
}
connection.close();// 關閉連結
return cords;// 擷取完畢,直接放回
}
public static boolean addcord(cord cord) throws Exception {// 增加一條記錄
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection connection = connection = DriverManager.getConnection(url,
"A", "B");
String sql = "insert into recored values (null,?,?,?)";// 這裡之是以寫null,是因為我的id字段設定的自動遞增
// data content expression分别與三個問号相對應
PreparedStatement preparedStatement = connection.prepareStatement(sql);// 同上,預編譯
preparedStatement.setString(1, cord.getData());
preparedStatement.setString(2, cord.getcontents());// 設定第二個問号的值,sql中
preparedStatement.setString(3, cord.getExpression());
int n = preparedStatement.executeUpdate();// 對幾條記錄做了更新,操作,這裡就傳回個數
connection.close();// 關閉連接配接
if (n > 0) {// 表示添加更新成功
return true;
}
return false;
}
public static void pri() throws Exception {// 這個方法是将資料庫的内容讀取到文本域中,也可以了解為重繪
// 首先拿到資料裡的對象
msbordcon.setText("留言内容:\n");
ArrayList<cord> record = getSource();
for (int i = 0; i < record.size(); i++) {// 将裡面的每一條資料追加到留言闆,也就是文本域
msbordcon.append(record.get(i).toString());
msbordcon.setSelectionStart(msbordcon.getText().length());// 将光标移動至最後一行,友善後面追加資料
}
}
}
class cord {// 這個用來存放從資料庫裡面查到的結果
private int id;
private String data;// 日期
private String expression;// 表情
private String contents;// 内容
cord(int id, String data, String expression, String contents) {
this.id = id;
this.expression = expression;
this.data = data;
this.contents = contents;
}
public String getData() {
return data;
}
public String getExpression() {
return expression;
}
public String getcontents() {
return contents;
}
public String toString() {// 重寫,以便後面直接導出資料到文本聊天記錄闆
String temp = data + " " + "你" + expression + "地說:" + contents + "\n";
return temp;
}
}
運作結果如下: