Java数据库查询未连接

时间:2014-06-08 23:06:34

标签: java sql

由于某种原因,我的应用程序将无法连接,我不确定有什么问题,这里是我得到的错误,它显示了一些关于驱动程序连接错误的信息。我尝试连接的是JDBC-ODBC Bridge driver使用Class.forName()方法。

Exception in thread "main" 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:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
    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:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:243)
    at databasequery.DataBaseQuery.<init>(DataBaseQuery.java:32)
    at databasequery.DataBaseQuery.main(DataBaseQuery.java:24)

//代码在这里

package databasequery;

//imports
import java.sql.*;
import java.io.*;
import java.util.*;

public class DataBaseQuery{

    //variables
    static String FirstName;
    static String LastName;
    static String ID;
    static String Symbol;


    public static void main(String[] args)throws Exception{
        DataBaseQuery q = new DataBaseQuery();
    }

    public DataBaseQuery()throws Exception{
       //driver manager
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       //stockTracker URL
       //connection
       Connection Connection = DriverManager.getConnection("jdbc:odbc:StockTracker");

       //Connection Conn = DriverManager.getConnection(
       Statement Statement = Connection.createStatement();

       //Query
       ResultSet Result = Statement.executeQuery("SELECT * FROM Users");

       System.out.println("Stock by username");

       //loop through the results
       while(Result.next()){
           ID = Result.getString("userID");
           FirstName = Result.getString("firstName");
           LastName = Result.getString("lastName");
           System.out.println("ID: " + ID);
           System.out.println("First Name: " + FirstName);
           System.out.println("Last Name: " + LastName);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

来自docs

  

...将在JDK 8中删除JDBC-ODBC Bridge ... 。另外,甲骨文   不支持JDBC-ODBC Bridge。 Oracle建议您使用   数据库供应商提供的JDBC驱动程序而不是   JDBC-ODBC Bridge。