简体   繁体   English

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

[英]com.mysql.jdbc.driver class not found exception

I took help from this blog post: 我从这篇博客文章中获得了帮助:

But I get com.mysql.jdbc.driver class not found exception. 但是我发现com.mysql.jdbc.driver类未找到异常。 What's different in that blog post was that they've tried to connect to mysql instead of MS SQL in my case. 那篇博客文章的不同之处在于,在我的案例中,他们尝试连接到mysql而不是MS SQL。 Here's my code so far: package com.example.dbtry; 到目前为止,这是我的代码: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;
}

}

Please tell me what I'm doing wrong. 请告诉我我在做什么错。 I have also added permission to the Internet in the manifest. 我还在清单中添加了对Internet的许可。

Download the JTDS driver from here and include it in your classpath . 从此处下载JTDS驱动程序,并将其包含在您的classpath Build and run your code. 生成并运行您的代码。 It'll work. 会的

You're missing the Mysql Connection JAR on your classpath. 您在类路径中缺少Mysql Connection JAR。

This jar contains the driver: com.mysql.jdbc.Driver 这个jar包含驱动程序: com.mysql.jdbc.Driver

However as you are using MSSQL and not MySQL I suggest you find a suitable driver for MSSQL instead. 但是,当您使用MSSQL而不是MySQL时,建议您找到适合MSSQL的驱动程序。

I guess there is a problem with the url stated. 我猜说的网址有问题。

try running on the local host first and then give the correct url that you have. 请先尝试在本地主机上运行,​​然后提供正确的URL。 "jdbc:mysql://localhost:3306/databaseName" “JDBC:MySQL的://本地主机:3306 /的databaseName”

You are loading the class for MYSQL DB connection, you should load the class for MS SQL and should include the required jar files in the build path. 您正在加载用于MYSQL DB连接的类,您应加载用于MS SQL的类,并应在构建路径中包含所需的jar文件。 Edit the Class.forName line as below: 编辑Class.forName行,如下所示:

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

download the jar from: http://www.java2s.com/Code/Jar/s/Downloadsqljdbc430jar.htm then change these lines as below: 从以下位置下载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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM