Showing posts with label jdbc. Show all posts
Showing posts with label jdbc. Show all posts

Thursday, July 14, 2011

Android MySQL Connectivity (MySQL Connector)



Hope this method works. However its not recommended to use JDBC Connectivity for connections over wifi or 3g. So the best way to connect MySQL from android is to use JSON or XML Parsings

Step 1 Including MySQL Connector
Add this jar to the classpath. In Eclipse Right Click in the Project>BuildPath>Configure Build Path. Then a window should popup, click the libraries tab at the top, click add external jar and navigate to File System/usr/share/java/mysql-connector-java.jar

Step 2 Coding Part

JDBC, simply allows you to connect to your server from java.

1:  import java.sql.Connection;  
2:  import java.sql.DriverManager;  
3:  import java.sql.ResultSet;  
4:  import java.sql.Statement;  
5:  public class DB {  
6:  private static final String url = “jdbc:mysql://localhost/android”;  
7:  private static final String user = “root”;  
8:  private static final String password = “MySql Password”;  
9:  public static void main(String args[]) {  
10:  try {  
11:  Class.forName(“com.mysql.jdbc.Driver”);  
12:  Connection con = DriverManager.getConnection(url, user, password);  
13:  Statement st = con.createStatement();  
14:  ResultSet rs = st.executeQuery(“select * from User”);  
15:  while(rs.next()) {  
16:  Log.v("DB", rs.getString(2) )  
17:  }  
18:  } catch (Exception e) {  
19:  }  
20:  }  
21:  }