com.mysql.jdbc.driver类未找到exception

我从这篇博文中得到了帮助:

但我得到com.mysql.jdbc.driver类未找到exception。 在博客文章中有什么不同之处在于他们在我的案例中尝试连接到mysql而不是MS SQL。 到目前为止,这是我的代码:package 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; } } 

请告诉我我做错了什么。 我还在清单中添加了对Internet的权限。

从以下url下载jar: http : //www.java2s.com/Code/Jar/s/Downloadsqljdbc430jar.htm然后更改这些行,如下所示:

 private static final String url = "jdbc:sqlserver://Server.com:1433/DB_name"; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

从此处下载JTDS驱动程序并将其包含在classpath 。 构建并运行代码。 它会工作的。

你错过了类路径上的Mysql Connection JAR。

这个jar包含驱动程序: com.mysql.jdbc.Driver

但是,由于您使用的是MSSQL而不是MySQL,我建议您为MSSQL找到合适的驱动程序。

我猜这个url存在问题。

首先尝试在本地主机上运行,​​然后提供您拥有的正确URL。 “JDBC:MySQL的://本地主机:3306 /的databaseName”

您正在加载MYSQL数据库连接的类,您应该加载MS SQL的类,并且应该在构建路径中包含所需的jar文件。 编辑Class.forName行,如下所示:

 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();