java連接Access數(shù)據(jù)庫的方法
java連接Access數(shù)據(jù)庫的方法,分享給大家
步驟:
1.現(xiàn)在我們一般使用的編譯環(huán)境是java SE 1.8,不支持odbc的連接方式,所以可以用jdbc的連接方式,還要在網(wǎng)上下載一個(gè)jdbc的驅(qū)動包。(這里用了Access_JDBC30.jar包,在網(wǎng)上可以找到)
2.右擊JRE System Libary->點(diǎn)擊 Build Path->點(diǎn)擊Add External JARs->將Access_JDBC30.jar添加進(jìn)去。
3.在這些都準(zhǔn)備好之后,j建立數(shù)據(jù)庫,還要將Access數(shù)據(jù)庫的版本降為2000或者2003的版本。
連接數(shù)據(jù)庫代碼:
Class.forName("com.hxtt.sql.access.AccessDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:Access:///C:/Users/k05/Desktop/Address.mdb","",""); //數(shù)據(jù)庫路徑 用戶名 密碼
數(shù)據(jù)庫示例:
運(yùn)行結(jié)果:
代碼如下:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.*; /** * * @version 1.22 2017-4-7 * @author BeiMengMuXi */ public class ASTest { public static void main(String[] args) { new ASFrame(); } }
import java.sql.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class ASFrame extends JFrame{ private static final int DEFAULT_WEIDTH = 400; private static final int DEFAULT_HEIGHT = 680; private JScrollPane scpDemo; private JTable tabDemo; private JScrollPane scpDemo1; private JTable tabDemo1; public ASFrame() { JFrame f=new JFrame(); f.setTitle("通訊錄"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(DEFAULT_WEIDTH, DEFAULT_HEIGHT); f.setVisible(true); f.setResizable(false); f.setLayout(null); JLabel label1 = new JLabel("聯(lián)系人"); f.add(label1); label1.setFont(new Font("宋體",1,30)); label1.setBounds(150, 30, 100, 40); this.scpDemo = new JScrollPane(); this.scpDemo.setBounds(40,110,320,500); try { btnShow(); } catch (InstantiationException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 將組件加入到窗體中 f.add(this.scpDemo); } public void btnShow() throws InstantiationException, IllegalAccessException{ String sql = "select * from Address"; try{ // 獲得連接 Class.forName("com.hxtt.sql.access.AccessDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:Access:///C:/Users/k05/Desktop/Address.mdb","",""); PreparedStatement pstm = conn.prepareStatement(sql); ResultSet rs = pstm.executeQuery(sql); // 計(jì)算有多少條記錄 int count = 0; while(rs.next()){ count++; } rs = pstm.executeQuery(); // 將查詢獲得的記錄數(shù)據(jù),轉(zhuǎn)換成適合生成JTable的數(shù)據(jù)形式 Object[][] info = new Object[count][2]; count = 0; while(rs.next()){ info[count][0] = rs.getString("name"); info[count][1] = rs.getString("phonenumber"); count++; } // 定義表頭 String[] title = {"姓名","電話號碼"}; // 創(chuàng)建JTable this.tabDemo = new JTable(info,title); // 顯示表頭 //this.jth = this.tabDemo.getTableHeader(); // 將JTable加入到帶滾動條的面板中 this.scpDemo.getViewport().add(tabDemo); rs.close(); conn.close(); }catch(ClassNotFoundException cnfe){ JOptionPane.showMessageDialog(null,"數(shù)據(jù)源錯(cuò)誤","錯(cuò)誤",JOptionPane.ERROR_MESSAGE); }catch(SQLException sqle){ JOptionPane.showMessageDialog(null,"數(shù)據(jù)操作錯(cuò)誤","錯(cuò)誤",JOptionPane.ERROR_MESSAGE); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章

自動配置@EnableAutoConfiguration問題

java通過AOP實(shí)現(xiàn)全局日志打印詳解