0

我对 android 开发相当陌生,我正在尝试将我的应用程序连接到 SQL Server 2008 数据库。尝试连接时,我收到一条错误消息“找不到数据库驱动程序 net.sourceforge.jtds.jdbc.Driver”。这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_course);
        // Show the Up button in the action bar.
        setupActionBar();
        setCurrentDateOnView();
        addListenerOnButton();
        TextView textview7 = (TextView) findViewById(R.id.textView7);
        String connectionurl = "jdbc:jtds:sqlserver:winsqls01.cpt.wa.co.za;databaseName=Courses; user=*;Password=*;";
try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
        textview7.setText("Successful");
        Connection con = DriverManager.getConnection(connectionurl);
        // Create and execute an SQL statement that returns some data.
        String SQL = "SELECT * FROM Courses";
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(SQL);
        while(rs.next())
        {
            textview7.setText(rs.getString(0));
        }
} 

    catch (ClassNotFoundException e) {

         textview7.setText("Could not find the database driver " + e.getMessage());

               } catch (SQLException e) {          
                   textview7.setText("Could not connect to the database " + e.getMessage());

               }

catch (Exception e) {
    e.printStackTrace();
} 







        dpResult1 = (DatePicker) findViewById(R.id.dpResult1);
        dpResult1.setVisibility(View.GONE);
        dpResult2 = (DatePicker) findViewById(R.id.dpResult2);
        dpResult2.setVisibility(View.GONE);
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

我对驱动程序的导入是,import net.sourceforge.jtds.jdbc.*;但它说我实际上并没有使用这个导入。有人可以帮忙吗?

4

1 回答 1

1

你看过这个解决方案吗?

您使用的是哪个版本的 jdbc 驱动程序和哪个版本的 Java?用户 aiolos 建议返回旧版本 1.2.6 以使其正常工作。如果您使用的是 1.3.0,那么您也必须使用 Java 7。Java 6 及更低版本无法加载驱动程序。

更新

连接字符串应该是那种格式

 jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]

所以试试你的:

 jdbc:jtds:sqlserver://winsqls01.cpt.wa.co.za:__PORT__;databaseName=Courses;user=*;password=*;

更新 2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_course);
    // Show the Up button in the action bar.
    setupActionBar();
    setCurrentDateOnView();
    addListenerOnButton();
    TextView textview7 = (TextView) findViewById(R.id.textView7);
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
        textview7.setText("Successful");
        Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://winsqls01.cpt.wa.co.za:__PORT__;databaseName=Courses", "__USERNAME__", "__PASSWORD__");
        // Create and execute an SQL statement that returns some data.
        String SQL = "SELECT * FROM Courses";
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(SQL);
        while(rs.next())
        {
            textview7.setText(rs.getString(0));
        }
    } 

    catch (ClassNotFoundException e) {
        textview7.setText("Could not find the database driver " + e.getMessage());
    } 
    catch (SQLException e) {          
        textview7.setText("Could not connect to the database " + e.getMessage());
    }
    catch (Exception e) {
        textview7.setText("Some other error occured: " + e.getMessage());
        e.printStackTrace();
    }
} 

试试我上面的代码,它是你的代码重组并根据你的需要更改所有标有下划线的变量。另请注意,您确定数据库名称和表名称都等于“课程”吗?

于 2013-08-18T12:09:18.490 回答