我对 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.*;
但它说我实际上并没有使用这个导入。有人可以帮忙吗?