2011-12-24 198 views
0

我使用Java學習數據庫,這裏是將簡單的代碼:JDBC-ODBC橋連接

import java.awt.BorderLayout; 

import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.List; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextField; 


public class ClassGUIApplication extends JFrame{ 

private JPanel centerPnl = new JPanel(); 
private JPanel northPnl = new JPanel(); 
private JLabel idLbl = new JLabel("Id:"); 
private JLabel nameLbl = new JLabel("Name:"); 
private JLabel surnameLbl = new JLabel("Surname:"); 
private JLabel gradeLbl = new JLabel("Grade:"); 
private JTextField idTxt = new JTextField(30); 
private JTextField nameTxt = new JTextField(30); 
private JTextField surnameTxt = new JTextField(30); 
private JTextField gradeTxt = new JTextField(30); 
private JButton insertBtn = new JButton("INSERT"); 
private JTable table; 
private StudentDBManager mng = new StudentDBManager(); 
private ClassTableModel model; 
public ClassGUIApplication() { 
    setTitle("Class Grade Report"); 
    setLayout(new GridLayout(2,1)); 
    centerPnl.setLayout(new GridBagLayout()); 
    GridBagConstraints gc = new GridBagConstraints(); 
    gc.gridx = 0; 
    gc.gridy = 0; 
    centerPnl.add(idLbl, gc); 
    gc.gridx = 1; 
    gc.gridy = 0; 
    centerPnl.add(idTxt, gc); 
    gc.gridx = 0; 
    gc.gridy = 1; 
    centerPnl.add(nameLbl, gc); 
    gc.gridx = 1; 
    gc.gridy = 1; 
    centerPnl.add(nameTxt, gc); 
    gc.gridx = 0; 
    gc.gridy = 2; 
    centerPnl.add(surnameLbl, gc); 
    gc.gridx = 1; 
    gc.gridy = 2; 
    centerPnl.add(surnameTxt, gc); 
    gc.gridx = 0; 
    gc.gridy = 3; 
    centerPnl.add(gradeLbl, gc); 
    gc.gridx = 1; 
    gc.gridy = 3; 
    centerPnl.add(gradeTxt, gc); 
    gc.gridx = 2; 
    gc.gridy = 4; 
    centerPnl.add(insertBtn, gc); 
    insertBtn.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      int id = Integer.parseInt(idTxt.getText()); 
      String name = nameTxt.getText(); 
      String surname = surnameTxt.getText(); 
      double grade = Double.parseDouble(gradeTxt.getText()); 
      Student student = new Student(id,name,surname,grade); 
      mng.addStudent(student); 
      List<Student> classList = mng.getStudents(); 
      model = new ClassTableModel(classList); 
      table.setModel(model); 

     } 
    }); 

    northPnl.setLayout(new GridLayout()); 
    List<Student> classList = mng.getStudents(); 
    model = new ClassTableModel(classList); 
    table = new JTable(model); 
    northPnl.add(new JScrollPane(table)); 

    add(centerPnl); 
    add(northPnl); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setPreferredSize(new Dimension(500,500)); 
    setVisible(true); 
    pack(); 
} 

public static void main(String[] args) { 
    ClassGUIApplication app = new ClassGUIApplication(); 
} 


} 


public static void main(String[] args) { 
    ClassGUIApplication app = new ClassGUIApplication(); 
} 


} 

Eclipse中給人eror這樣的:

 
java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application 
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) 
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) 
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source) 
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source) 
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at StudentDBManager.openConnection(StudentDBManager.java:21) 
    at StudentDBManager.(StudentDBManager.java:15) 
    at ClassGUIApplication.(ClassGUIApplication.java:36) 
    at ClassGUIApplication.main(ClassGUIApplication.java:102) 
+0

'StudentDBManager.openConnection(StudentDBManager.java:21)'jdbc URL的外觀如何? – stacker 2011-12-24 14:21:14

+0

請爲您的StudentDBManager類別提供代碼。這可以讓更多的燈光和實際嘗試與您的數據源連接的代碼。 – 2011-12-24 14:21:40

+0

@ user1091692:您使用的是64位操作系統嗎? – 2011-12-24 14:29:46

回答

3

是你的操作系統或JVM 32位或64位?

我不相信64位JVM具有ODBC橋驅動程序類。和一個64位操作系統會建議你應該閱讀:

http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/1c63e3ae-e001-4066-9eac-ad9162116603

每當我得到一個錯誤,我做的第一件事是將其粘貼到谷歌。我通常知道我不是第一個看到我的問題的人。

+0

我的操作系統和JRE是64位,我嘗試使用32位odbcad cuz 64位不支持 – Beyaz 2011-12-24 14:39:49

+0

謝謝我解決了我的問題與您的鏈接 – Beyaz 2011-12-25 14:12:23

+0

聖誕快樂!我更願意接受我的答覆並表示感謝。 – duffymo 2011-12-25 15:41:57