學習筆記
1.用函式的方式讀取ACCESS資料庫,並顯示在視窗上
2.使用JScrollPane讓textArea可以顯示捲軸
3.scrollPane捲軸的設定,橫向與縱向是否要使用捲軸
4.按鈕事件後,讓textArea去讀取函式抓新的值 (try語法)
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class onioni{
private JFrame frame;
/**
* Launch the application.
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
onioni window = new onioni();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public onioni() throws SQLException{
initialize();
output_list();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() throws SQLException{
frame = new JFrame();
frame.setBounds(100, 100, 536, 514);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
}
public static String output_text(int id) throws SQLException{ //輸出文字
Connection connDB = null;
String X=null;
try
{
//建立驅動程式,連結odbc至Microsoft Access
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
}catch(ClassNotFoundException e)
{
System.out.println("Driver loading failed!");
}
//連結資料庫
connDB = DriverManager.getConnection("jdbc:ucanaccess://f:/text.mdb");
Statement st = connDB.createStatement();
st.execute("SELECT * FROM 文章 where 識別碼="+id);
ResultSet rs = st.getResultSet();
while(rs.next()) //將數據讀出來
{
X=rs.getString("內容");
}
return X; //轉出
}
private void output_list() throws SQLException{
//下拉式選單
JComboBox comboBox = new JComboBox();
comboBox.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(comboBox.getSelectedItem().toString());
}
});
comboBox.setBounds(61, 36, 100, 21);
frame.getContentPane().add(comboBox);
//表單內容
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true); //設定自動換行
final JScrollPane scrollPane = new JScrollPane(textArea);
// textArea.setBounds(107, 100, 270, 286); //不可以設定textArea的大小,不然無法顯示捲軸
frame.getContentPane().add(scrollPane); //這邊要設定add(scrollPane),不是add(textArea)
scrollPane.setBounds(50, 100, 400, 300);
scrollPane.setHorizontalScrollBarPolicy( //橫向的不出現捲軸
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy( //縱向的有需要再出現捲軸
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
textArea.setText(output_text(228));
//按鈕事件
JButton btnNewButton = new JButton("\u78BA\u5B9A");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int i= Integer.parseInt(comboBox.getSelectedItem().toString()); //強制轉成數字
try { //要用try才可以
textArea.setText(output_text(i));
} catch (SQLException e1) {
// TODO 自動產生的 catch 區塊
e1.printStackTrace();
}
}
});
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton.setBounds(171, 35, 87, 23);
frame.getContentPane().add(btnNewButton);
//連結資料庫的程式
Connection connDB = null;
try
{
//建立驅動程式,連結odbc至Microsoft Access
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
}catch(ClassNotFoundException e)
{
System.out.println("Driver loading failed!");
}
//連結資料庫
connDB = DriverManager.getConnection("jdbc:ucanaccess://f:/text.mdb");
//SQL共有 INSERT、SELECT、UPDATE、DELETE,以下分別列舉
Statement st = connDB.createStatement();
st.execute("SELECT * FROM 文章");
ResultSet rs = st.getResultSet();
while(rs.next())
{
comboBox.addItem(rs.getString("識別碼"));
}
}
}
沒有留言:
張貼留言