Reputation: 2803
I am using Eclipse in Windows and i want to connect to a MySql DB. I know i should use JDBC. But i don't know how to use it!
I have downloaded both msi and zip file from JDBC download page.
In the zip file, a file with this name exsits : mysql-connector-java-5.1.30-bin.jar What should i do with this?
I mean where i copy/import (in a folder or in eclipse) this file?
Note that i have this code:
import android.os.Bundle;
import android.app.Activity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LearningActivity extends Activity {
private static final String url = "jdbc:mysql://cool/app";
private static final String user = "user";
private static final String pass = "password";
private Button button;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)this.findViewById(R.id.button1);
tv = (TextView)this.findViewById(R.id.textView1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Connect();
}
});
}
public void Connect() {
Connect task = new Connect();
task.execute();
}
private class Connect extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
String result = "Database connection success\n";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from users");
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());
}
return response;
}
@Override
protected void onPostExecute(String result) {
tv.setText(result);
}
}
}
But I get this error : ClassNotFoundException : com.mysql.jdbc.Driver
Upvotes: 0
Views: 716
Reputation: 11185
An offline discussion with @Ali revealed that he was using an old version of the ADT that does not create the libs
folder as shown in the folder hierarchy on the android developers web site
Manually creating this folder solved the problem for him.
Note: The name of the folder is libs with a "s" at the end. It is easy to miss that and create lib
instead.
Upvotes: 2
Reputation: 15052
I place these in a lib directory inside my eclipse project, then add the jar to my build path.
Upvotes: 0