gpt4 book ai didi

java - 新手 Android 开发人员尝试连接到 SQL Server 2008

转载 作者:行者123 更新时间:2023-12-01 14:13:38 25 4
gpt4 key购买 nike

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

最佳答案

你看过这个吗solution

您使用的是哪个版本的 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();
}
}

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

关于java - 新手 Android 开发人员尝试连接到 SQL Server 2008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18298798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com