Recently I have tried to connect to SQL Database server which is in my local network machine. I can connect and access SQL server from my Android app. I did it in the following way….
1. First of all you need a JDBC driver library for SQL Server. As we know android library has only SQLite database driver. So first download an open source JDBC driver from this
http://jtds.sourceforge.net/ site (I downloaded the Linux version).
2. Then import the jar file into your Android app.(jtds-1.2.5.jar).
3. Now just try this code by modifying according to your context
01 | import java.sql.Connection; |
02 | import java.sql.DriverManager; |
03 | import java.sql.ResultSet; |
04 | import java.sql.Statement; |
06 | import net.sourceforge.jtds.jdbc.*; |
10 | Log.i("Android"," MySQL Connect Example."); |
11 | Connection conn = null; |
13 | String driver = "net.sourceforge.jtds.jdbc.Driver"; |
14 | Class.forName(driver).newInstance(); |
16 | String connString = "jdbc:jtds:sqlserver://server_ip_address:1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;
password=xxxxxxxx;instance=SQLEXPRESS;"; |
17 | String username = "xxxxxx"; |
18 | String password = "xxxxxxxxxx"; |
19 | conn = DriverManager.getConnection(connString,username,password); |
20 | Log.w("Connection","open"); |
21 | Statement stmt = conn.createStatement(); |
22 | ResultSet reset = stmt.executeQuery("select * from TableName"); |
26 | Log.w("Data:",reset.getString(3)); |
33 | Log.w("Error connection","" + e.getMessage()); |
Hi,
ReplyDeleteAm also trying the same. But not able to connect.
Is it possible to share the sample project?
Where to write the above code???
ReplyDelete