Reputation: 373
My source code as below:
private static final String url = "jdbc:mysql://localhost/db_name";
private static final String user = "xxxx";
private static final String pswd = "xxxx";
private static final String sql = "SELECT * FROM table_name";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout linLayMain = new LinearLayout (this);
TextView txtViewMainTitle = new TextView (this);
TextView txtViewAdminName = new TextView (this);
TextView txtViewAdminPswd = new TextView (this);
txtViewMainTitle.setText ("Main Title : NONE");
txtViewAdminName.setText ("Admin Name : NONE");
txtViewAdminPswd.setText ("Admin Password : NONE");
try
{
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection con = (Connection) DriverManager.getConnection (url, user, pswd);
txtViewMainTitle.setText ("Main Title : JDBC Connection Sccess");
Statement st = con.createStatement ();
ResultSet rs = st.executeQuery (sql);
while (rs.next ())
{
txtViewAdminName.setText ("Admin Name : " + rs.getInt (1));
txtViewAdminPswd.setText ("Admin Pswd :" + rs.getString (2));
}
}
catch (Exception e)
{
Log.i ("Test GoSport", "Jdbc Error : " + e.getMessage());
e.printStackTrace ();
}
linLayMain.addView (txtViewMainTitle);
linLayMain.addView (txtViewAdminName);
linLayMain.addView (txtViewAdminPswd);
txtViewMainTitle.setLayoutParams (new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txtViewAdminName.setLayoutParams (new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txtViewAdminPswd.setLayoutParams (new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView (linLayMain);
}
But, it never success before now. When I saw the Log.i in DDMS, it always shows "JDBC Connection Error".
My Environment as below:
Mac OS X 10.7.2, Eclipse Java EE Indigo, mysql-connection-java-5.1.18-bin.jar, emulator 4.0.3, API 15
Upvotes: 1
Views: 2969
Reputation: 7347
This isn't really an answer but I remember when I first started programming in Android I was told not to make direct connection to remote databases, but to always go through an interface (ie JSP, PHP, .NET, etc...). I'm not sure of the exact reason for this (probably security) but just thought I'd throw in my 2 cents
Upvotes: 1