2011年4月19日
<Java> JVA405-資料庫瀏覽器
這題重點:
1. 使用JAVA讀取MDB資料庫
2. 在JAVA中使用SQL語法,搜尋資料庫
3. 表格的建立
==========================
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.applet.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;
import java.io.*;
import java.util.*;
import java.sql.*;
public class JVA405 extends JFrame {
JButton jbutton = new JButton();
JTable jtable = new JTable();
JTextField jtextfield = new JTextField();
// Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch(Exception e) {
e.printStackTrace();
}
new JVA405();
}
// 建構函式
public JVA405() {
super("資料庫瀏覽器");
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JLabel jlabel = new JLabel("SQL指令:");
jtextfield.setText("select * from Customers");
jbutton.setSelected(true);
jbutton.setText("執行SQL");
jbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jbutton_actionPerformed(e);
}
});
// 捲軸
//////////////////////////////////////////////////
JScrollPane jscrollpane = new JScrollPane(jtable,20,30);
//////////////////////////////////////////////////
JPanel jpanel = new JPanel(new BorderLayout());
jpanel.add(jlabel, BorderLayout.WEST);
jpanel.add(jtextfield, BorderLayout.CENTER);
jpanel.add(jbutton, BorderLayout.EAST);
contentPane.add(jpanel, BorderLayout.NORTH);
contentPane.add(jscrollpane, BorderLayout.CENTER);
// Center the frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
this.setLocation((screenSize.width - frameSize.width) 2, (screenSize.height - frameSize.height) 2);
this.validate();
this.setSize(new Dimension(400, 300));
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
void jbutton_actionPerformed(ActionEvent e) {
////////////////////////////////////////////////////
String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver" ;
String dbURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=JVA04.mdb" ;
Connection con = null;
ResultSet rs = null;
DefaultTableModel dtm = new DefaultTableModel();
jtable.setModel(dtm);
//jtable.setAutoResizeMode(0);
String query = jtextfield.getText();
if(query.trim().equals("")){
JOptionPane.showMessageDialog(this,"請輸入SQL指令","資料庫瀏覽器",0);
return;
}
try {
Class.forName(dbDriver); //載入JDBC驅動程式
con = DriverManager.getConnection(dbURL); //取得與資料庫的連線
Statement s = con.createStatement(); //利用Statement物件,用來存放進度搜尋用的SQL指令
//執行搜尋指令 存放至ResultSet
rs = s.executeQuery(query);
//if(s.execute(query)){
// rs = s.getResultSet();
ResultSetMetaData rsm = rs.getMetaData();
int col = rsm.getColumnCount();
for(int i = 1 ; i <=col ; i++){
dtm.addColumn(rsm.getColumnLabel(i));
}
String as[] = new String[col];
while(rs.next()){
for(int i = 1; i<=col ; i++){
String str = rs.getString(i);
if(str == null)
str="";
as[i-1] = str;
}
dtm.addRow(as);
}
//}
}catch(SQLException sq1){
JOptionPane.showMessageDialog(this,"找不到資料","資料庫瀏覽器",0);
return;
}
catch (Exception ex1) {
ex1.printStackTrace();
}
finally {
try {
if(rs!=null)
rs.close();
con.close();
}
catch (Exception ex2) {
ex2.printStackTrace();
}
}
///////////////////////////////////////////////////////
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言