How to connect Android app to a SQL Server database?
I want to make an Android login that connects to sql server but I am confused to make a connection from Android to sql server
First, download the driver from this link and place it at the following address
https://sourceforge.net/projects/jtds/
Next you need to introduce the location of the above file in Gardel:
Complite file ('libs / jtds-1.3.1.jar)
Then create a class with the desired name and in it create values (username and password and database name and IP name) of the string type
public class sqlConnectionToSQL {
// Database Username
String userName;
// Database Password
String password;
// Connection builder class
String classes;
// Site or Local IP
String ip;
}
Then create the connection function in this way and put the following code in it
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder ();
StrictMode.setThreadPolicy (policy);
Connection conn = null;
String connURL = null;
try
{
Class.forName (classes)
connURL = "jdbc: jtds: sqlserver: //" + ip + ": 1433" + "; databasename:" + db + "; Username:" + userName + "; Password:" + password + ";";
conn = DriverManager.getConnection (connURL);
} Catch (ClassNotFoundException e) {
Log.e (e.getMessage (), "Error:");
} Catch (sqlException e) {
Log.e (e.getMessage (), "Error:");
}
Connection Catch (Exception e) {
Log.e (e.getMessage (), "Error:");
}
return conn;
}
This is the way to connect the database to Android
Related
i am using jdbc driver library to connect my android app to sql server 2014.it gives me error sometimes while executing query.Error is mention below
cannot open database requested by the login. the login failed for user
Here is code for connecting android app to sql server 2014
String connectionURL;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connectionURL = "jdbc:jtds:sqlserver://"
+ user.getServerIP()
+ ";databaseName="
+ user.getDbName()
+ ";user="
+ user.getUserName()
+ ";password="
+ user.getPassword()
+ ";";
Connection con = DriverManager.getConnection(connectionURL);
con.close();
return con;
} catch (Exception e) {
Timber.e(e.getMessage());
return null;
}
Please help me out.
I'm trying to Make a connection android studio to the database in SQL server 2014 , But this error appears :
java.sql.SQLException: Invalid object name 'tablename'
I use the :jtds 1.3.1
and :sqljdbc4-2.0
I connect a local network .
The SQL statement is failing because you are not using the correct connection URL format for jTDS so you are not actually connecting to the database specified by the String variable serverDb.
You are trying to use a connection URL parameter named database that jTDS does not recognize:
String serverDb = "myDb";
String connUrl = "jdbc:jtds:sqlserver://localhost:49242;database=" + serverDb;
try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
System.out.println(conn.getCatalog()); // prints: master
} catch (Exception e) {
e.printStackTrace(System.err);
}
Instead, you should be using the server:port/database format described in the documentation
String serverDb = "myDb";
String connUrl = "jdbc:jtds:sqlserver://localhost:49242/" + serverDb;
try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
System.out.println(conn.getCatalog()); // prints: myDb
} catch (Exception e) {
e.printStackTrace(System.err);
}
I am working on an Android App for my friend database class and I am a bit in a bind. I am having troubles establishing my connection. Can someone assist me with this? I have a local database, I don't know much about MS SQL server. Here is the information:
ip = "Ip address"; // i am inserting IPV4 IP of computer in this
db = "testing";
un = "xee";
pass = "1995";
port = "1433";
#SuppressLint("NewApi")
public Connection connectionclass(String user, String password, String database, String server)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
ConnectionURL = "jdbc:jtds:sqlserver://" + ip +":"+port+";"+ "databaseName=" + db + ";user=" + un + ";password="+ password + ";";
}
catch (SQLException se)
{
Log.e("error here 1 : ", se.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("error here 2 : ", e.getMessage());
}
catch (Exception e)
{
Log.e("error here 3 : ", e.getMessage());
}
return connection;
}
}
I am getting an error when trying to connect
E/error here 1 :: Network error IOException: failed to connect to /***.***.*.*** (port 1433) from /:: (port 35033): connect failed: ECONNREFUSED (Connection refused)
Is the user name / password definitely correct?
Is your SQL Server configured to 'mixed' authentication mode? https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/change-server-authentication-mode?view=sql-server-2017
Does the user account have sufficient permissions to access the database? Try setting the login account to be 'sysadmin', which will give it maximum privileges.
Check the Firewall (not if it's a local DB)
I am trying to connect my android app to my azure DB, to pull some data down. I have followed some posts from the internet; which gave me the below code. Now I know I have to add the jdbc driver to sql server to my project; but even when I do, I still get the above message.
Can anyone give me some advice?
CODE:
try {
// SET CONNECTIONSTRING
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
String username = "XXXXXXXXX";
String password = "XXXXXX";
Connection DbConn = DriverManager.getConnection("Connection");
Log.w("Connection","open");
Statement stmt = DbConn.createStatement();
ResultSet reset = stmt.executeQuery(" select Category FROM [dbo].[Feeds]");
Toast toast = Toast.makeText(getApplicationContext(), "Yes", Toast.LENGTH_LONG);
toast.show();
//EditText num = (EditText) findViewById(R.id.displaymessage);
// num.setText(reset.getString(1));
DbConn.close();
} catch (Exception e)
{
Toast toast = Toast.makeText(getApplicationContext(), "Error connection: " + e.getMessage(), Toast.LENGTH_LONG);
toast.show();
//Log.w("Error connection","" + e.getMessage());
}
I am trying to make an android (2.3) connection with MySQL. The code is below:
public class ConnectionClass {
static String user = "root";
public static void main() {
String url = "jdbc:mysql://127.0.0.1:3306/mydatabase";
// String url = "jdbc:mysql://10.2.5.69:3306/test";
try {
Class.forName("com.mysql.jdbc.Driver");
***Connection con = DriverManager.getConnection(url, user, "mypassword");***
con.isReadOnly();
System.out.println("success");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from user");
while (rs.next()) {
System.out.println("id" + rs.getInt(1));
System.out.println("data" + rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
but at the line Connection con = DriverManager.getConnection(url, user, "mypassword") I get the exception:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Well I didnt find anything wrong with your code..
But if you want to connect to mysql at any cost,I would suggest an alternate method of using php as an mediator, which can do all the operations which is mentioned and accessing this php file though HttpClient in java
ref:http://www.helloandroid.com/tutorials/connecting-mysql-database
Android doesn't support for MySQL database.
See the official documentation here:
http://developer.android.com/guide/topics/data/data-storage.html#db