gpt4 book ai didi

java - com.mysql.jdbc.driver 类未找到异常

转载 作者:搜寻专家 更新时间:2023-10-30 23:15:11 28 4
gpt4 key购买 nike

我从 this 得到了帮助博文:

但我得到 com.mysql.jdbc.driver class not found 异常。该博客文章的不同之处在于,在我的案例中,他们尝试连接到 mysql 而不是 MS SQL。到目前为止,这是我的代码:包 com.example.dbtry;

public class MainActivity extends Activity {
protected TextView tv;

private static final String url = "jdbc:jtds:sqlserver://Server.com:1433/DB_name";
private static final String user = "username";
private static final String pass = "password";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testDB();
}

public void testDB() {
tv = (TextView)findViewById(R.id.textView1);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
/* System.out.println("Database connection success"); */

String result = "Database connection success\n";
tv.setText(result);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from this_table");
ResultSetMetaData rsmd = rs.getMetaData();

while(rs.next()) {
result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
}
tv.setText(result);
}
catch(Exception e) {
e.printStackTrace();
tv.setText(e.toString());
}

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

请告诉我我做错了什么。我还在 list 中添加了访问互联网的权限。

最佳答案

下载JTDS驱动程序并将其包含在您的 classpath 中。构建并运行您的代码。它会起作用。

关于java - com.mysql.jdbc.driver 类未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15273713/

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