2013-11-26 44 views
4

當我試圖運行我的數據庫程序時,出現以下錯誤消息。這是我從我理解的問題中獲得的文件之一。數據庫 - [Microsoft] [ODBC驅動程序管理器]未找到數據源名稱並且沒有指定默認驅動程序

在此先感謝您的幫助!

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964) 
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121) 
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3080) 
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323) 
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174) 
    at java.sql.DriverManager.getConnection(DriverManager.java:571) 
    at java.sql.DriverManager.getConnection(DriverManager.java:233) 
    at data.DbManager.getAccessDbConnection(DbManager.java:201) 
    at data.DbManager.<init>(DbManager.java:26) 
    at user.Frame.<init>(Frame.java:10) 
    at user.MainP8.main(MainP8.java:16) 

DbManager.java

package data; 

import java.sql.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JFileChooser; 
import javax.swing.JOptionPane; 

public class DbManager { 

//Add to beginning of MS Access DB URL 
private String ACCESS_DB_URL_PREFIX = 
    "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; 
//Add to end of MS Access DB URL 
private final String ACCESS_DB_URL_SUFFIX = ";DriverID=22;READONLY=false;}"; 
//File name of database 
private final String MY_DB_NAME = "WebsiteDatabase.mdb"; 

private String fileName; 
private Connection myConnection; 

//constructor 
public DbManager() { 
    try { 
     myConnection = getAccessDbConnection(MY_DB_NAME); 
     myConnection.setAutoCommit(true); 
     DatabaseMetaData md = myConnection.getMetaData(); 
    } catch (SQLException ex) { 
     Logger.getLogger 
        (DbManager.class.getName()).log(Level.SEVERE, null, ex); 
     JOptionPane.showMessageDialog(null, 
      "The database could not be located. Please select the database" 
      + " file you wish to connect to.", 
      "Database Error", JOptionPane.ERROR_MESSAGE); 
     JFileChooser chooser = new JFileChooser(); 
     chooser.showOpenDialog(chooser); 
     fileName = chooser.getSelectedFile().toString(); 
     try { 
      myConnection = getAccessDbConnection(fileName); 
      myConnection.setAutoCommit(true); 
      DatabaseMetaData md = myConnection.getMetaData(); 
     } catch (SQLException ex1) { 
      Logger.getLogger 
        (DbManager.class.getName()).log(Level.SEVERE, null, ex1); 
      JOptionPane.showMessageDialog(null, 
       "The database could not be opened", "Fatal Error", 
       JOptionPane.ERROR_MESSAGE); 
     } 
    } 
} 

//"destructor" method to release the database connection 
public void close() { 
    try { 
     myConnection.close(); 
    } catch (SQLException ex) { 
     Logger.getLogger 
        (DbManager.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

//public methods to access the database 
public void insert(Website w) throws SQLException { 
    String sql; 
    //build SQL statement 
    sql = "INSERT INTO Websites"; 
    sql += " (COMPANY_NAME, COMP_ASSETS, YR_FOUNDED, URL_ADD, ALEXA_RANK)"; 
    sql += " VALUES ("; 
    sql += "'" + w.getCompName() + "',"; 
    sql += w.getAssets() + ","; 
    sql += " #" +w.getFounded() + "#,"; 
    sql += " '" + w.getUrl() + "',"; 
    sql += w.getAlexaRank() + ");"; 
      insertRecord(sql); 
} 
public void update(Website w) throws SQLException { 
    String sql; 
      //SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd"); 
      //date.parse(w.getFounded()); 
//    "#" 
    //build SQL statement 
    sql = "UPDATE WebsiteS SET"; 
    sql += " COMPANY_NAME = '" + w.getCompName() + "',"; 
    sql += " COMP_ASSETS = " + w.getAssets() + ","; 
    sql += " YR_FOUNDED = #" + w.getFounded() + "#,"; 
    sql += " URL_ADD = '" + w.getUrl() + "',"; 
      sql += " ALEXA_RANK = " + w.getAlexaRank() ; 

    sql += " WHERE ID = " + w.getId() + ";"; 
    updateRecord(sql); 
} 
public void delete(Website w) throws SQLException { 
    String sql; 
    sql = "DELETE * FROM Websites WHERE ID = " + w.getId() + ";"; 
    deleteRecord(sql); 
} 
public String[] getWebsiteList() throws SQLException { 
    String strSql = "SELECT COMPANY_NAME FROM Websites;"; 
    PreparedStatement ps = myConnection.prepareStatement(strSql, 
     ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 

    ResultSet rs = ps.executeQuery(); 

    rs.last(); 
    int rowCount = rs.getRow(); 
    String[] items = new String[rowCount]; 

    try { 
     rs.beforeFirst(); 
     int i = 0; 
     while(rs.next()) { 
      items[i] = rs.getString("COMPANY_NAME"); 
      i++; 
     } 
    } catch (Exception ex){ 
     JOptionPane.showMessageDialog(null, 
      "getWebsiteList: Unable to read website names: " + ex.getMessage()); 
     System.out.println(ex.getStackTrace()); 
     System.out.println(ex.getLocalizedMessage()); 
    } 

    return items; 
} 
public int[] getWebsiteIds() throws SQLException { 
    int[] id; 
    String strSql = "SELECT ID FROM Websites;"; 
    PreparedStatement ps = myConnection.prepareStatement(strSql, 
     ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 

    ResultSet rs = ps.executeQuery(); 

    rs.last(); 
    int rowCount = rs.getRow(); 
    id = new int[rowCount]; 

    try { 
     rs.beforeFirst(); 
     int i = 0; 
     while(rs.next()) { 
      id[i] = rs.getInt("ID"); 
      i++; 
     } 
    } catch (Exception ex) { 
     JOptionPane.showMessageDialog(null, 
      "getWebsiteIDs: Unable to read Website IDs: " + ex.getMessage()); 
     System.out.println(ex.getStackTrace()); 
     System.out.println(ex.getLocalizedMessage()); 
    } 

    return id; 
} 
public Website getWebsite(int wId) throws SQLException { 
    String[] rec; 
    String strSql = "SELECT * FROM Websites WHERE ID = " + wId + ";"; 
    Website website = null; 
    PreparedStatement ps = myConnection.prepareStatement(strSql, 
     ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 
    ResultSet rs = ps.executeQuery(); 
    ResultSetMetaData rsMeta = rs.getMetaData(); 
    int columns = rsMeta.getColumnCount(); 
    rec = new String[columns]; 
    try { 
     rs.beforeFirst(); 
     while(rs.next()) { 
      for (int i = 0; i < columns; i++) { 
       rec[i] = rs.getString(i + 1); 
      } 
     } 

     //use the data to build the Website object 
     website = new Website(
      //Integer.parseInt(rec[0]), 
      rec[0], 
      rec[1], 
      rec[2], 
      rec[3], 
          rec[4], 
          rec[5] 
     ); 
    } catch (SQLException ex) { 
     System.out.println(ex.getStackTrace()); 
     System.out.println(ex.getLocalizedMessage()); 
    } 
    return website; 
} 

//private method to establish database connection 
private Connection getAccessDbConnection(String fileName) 
                 throws SQLException { 
    try { 
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
    } catch (ClassNotFoundException ex) { 
     System.err.println("JdbcOdbc Bridge Driver not Found"); 
     JOptionPane.showMessageDialog(null, ex.getMessage(), "Driver Error", 
      JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 

    String databaseURL = ACCESS_DB_URL_PREFIX + fileName 
                + ACCESS_DB_URL_SUFFIX; 
    return DriverManager.getConnection(databaseURL); 
} 

//private methods to access the database 
private void insertRecord(String strSql) throws SQLException { 
    Statement st = myConnection.createStatement(); 
    try { 
     st.execute(strSql); 
    } catch (SQLException ex) { 
     System.err.println(ex.getStackTrace()); 
     System.err.println(ex.getMessage()); 
     System.err.println(ex.getLocalizedMessage()); 
    } 
    st.close(); 
} 
private void updateRecord(String strSql) throws SQLException { 
    //use prepared statement to ensure that the result set is editable 
    PreparedStatement ps = myConnection.prepareStatement(strSql, 
     ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 
    try { 
     ps.execute(); 
    } catch (SQLException ex) { 
     System.err.println(ex.getStackTrace()); 
     System.err.println(ex.getMessage()); 
     System.err.println(ex.getLocalizedMessage()); 
    } 
} 
private void deleteRecord(String strSql) throws SQLException { 
    PreparedStatement ps = myConnection.prepareStatement(strSql, 
     ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 
    try { 
     ps.execute(); 
    } catch (SQLException ex) { 
     System.err.println(ex.getStackTrace()); 
     System.err.println(ex.getMessage()); 
     System.err.println(ex.getLocalizedMessage()); 
    } 
} 
} 
+0

檢查這有助於:http://stackoverflow.com/questions/19334723/how-to -connect-to-ms-access-database 此外,您將不得不提供Db的完整路徑,而不是使用Db名稱,例如'C:\\ test \\ Db \\ WebsiteDatabase.mdb' –

+0

這沒有用。仍然得到相同的錯誤。 – Mario

回答

6

檢查

  1. 你有MS Access驅動程序安裝

  2. 您使用的描述字符串是正確的,「Microsoft Access驅動程序( .mdb)「它必須是確切的,請檢查驅動程序選項卡中ODBC控制面板應用程序,我的例子是( .mdb,* .accdb)

  3. 檢查你是不是混合64位JVM與32位驅動程序,反之亦然。

+0

看起來像它的一個64位JVM。我將如何獲得32位? – Mario

+1

是的,它看起來像* .mdb的第一個字符串與32位JVM一起工作,後者與* .mdb和* .accdb一起工作與64位驅動程序。注意我的代碼在前面的例子和前面的例子不一樣。 –

+0

這確實是32位JVM,它爲我解決了它。 –

3

如果您使用的是32位操作系統,那麼我認爲就沒有這樣的問題,但如果您使用的是64位的操作系統,然後按照下列步驟:

  1. 下載來自here的64位訪問驅動程序。
  2. 運行安裝程序,然後重新啓動計算機。
  3. 轉到開始 - >控制面板 - >管理工具 - >(數據源)ODBC - >系統DSN。
  4. 點擊「添加」按鈕。
  5. 選擇「Microsoft Access驅動程序(* .mdb)」
  6. 提供任何數據源名稱,然後單擊「創建」。
  7. 選擇您希望您的數據庫的目錄(注:該目錄應該是相同的源目錄,在Java程序的目錄)64位數據庫連接的執行
  8. 啓動命令提示符。
  9. 將C:\ WINDOWS \ SYSWOW64.exe複製到您計算機的運行對話框中,然後開始編譯並執行您的數據庫程序。
1

當我將筆記本電腦從32位Windows XP升級到64位Windows 7時,我的某個網站出現此問題。我通過創建32位應用程序池(應用程序池>高級設置>啓用32位應用程序=真),並以這種方式運行我的網站。即使安裝了Microsoft Office,也不會安裝用於64位站點訪問的驅動程序。希望這可以幫助別人

0

如果您使用的是64位操作系統使用,然後按照下列步驟:

Download 64-bit access driver from http://www.microsoft.com/en-in/download/details.aspx?id=13255. 

Run setup, then restart your computer. 

If you are an application developer using ODBC to connect to Microsoft Office Access data, set the Connection String to 「Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path to mdb/accdb file」 

If you are an application developer using ODBC to connect to Microsoft Office Excel data, set the Connection String to 「Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=path to xls/xlsx/xlsm/xlsb file」 
相關問題