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.
Related
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)
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
I'm new to android. When I try to connect my android app to MS SQL Server database using eclipse android emulator, the LogCat showed me the exception "Error Connection - NULL". I've searched many posts over the internet related to this issue but yet to get a solution for that.
Here is my code which try to connect to database after i click a button. I have copy jtds-1.2.7.jar into the libs folder. Also, set the permission to INTERNET in my manifest file. Can anyone let me know what's wrong with that? Please ~~
public void onClick1(View view) {
Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
String connString = "jdbc:jtds:sqlserver://serverIP:1435/db_name;encrypt=fasle;user=xxxxxx;password=!xxxxxx;instance=SQLEXPRESS;";
String username = "xxxxxx";
String password = "!xxxxxx";
conn = DriverManager.getConnection(connString,username,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select * from xxxxxx");
//Print the data to the console
while(reset.next()){
Log.w("Data:",reset.getString(3));
}
conn.close();
} catch (Exception e)
{
Log.w("Error connection","" + e.getMessage());
}
}
LogCat - After throw a lot of garbage, showing Error Connection, NULL
I'm trying to connect my android application to my oracle 10g DB, i'm using JDK 1.6, OJDBC14.jar and working on a 4.+ application. I used to have a few problem with the driver but here comes the best one : after excecuting this code :
String url="jdbc:oracle:thin:usr/pwd#r2d2.iut-orsay.u-psud.fr:1521:etudom";
Connection co = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
popUp("Driver Ok");
}
// Should never happend
catch (ClassNotFoundException e){
popUp("ERROR: Driver's missing");
//System.exit(1);
}
// Unknow problem
catch (Exception e){
popUp("ERROR : " + e.getMessage() + "\nFrom: " + e.getCause());
}
try {
co = DriverManager.getConnection(url);
popUp("Connection ok");
}
// In case of internet problem
catch (SQLException e) {
popUp("Throw from SQLException\nERROR : " + e.getMessage());
//System.exit(1);
}
// unknown problem
catch (Exception e){
popUp("ERROR from getConnection :\nMessage: " + e.getMessage() + "\nFrom: " + e.getCause() + "\nMessage extra: "+e.getLocalizedMessage());
}
And I've got a popUp like that :
ERROR from getConnection :
Message: null
From: null
Message extra: null
No output in the log or in the console.
I'm literaly lost
Any help would be great!
Ps: sorry for my bad english.
The reason why you are not able to connect the device to your Oracle DB is, mobile devices are not equipped to carry out such actions as they are not built with drivers for the vast amount of Databases that are out there. However, in order to connect your app to the Oracle DB you have to create what is called a Web Service or an API to get the job done. Check out these tutorials they should get you rolling:
How to connect Android with PHP, MySQL (the concept here is the same, just use your Oracle DB instead)
Oracle Mobile Database server
Android to Oracle connectivity through java
I am working with android 4.1.1, IDE= eclipse, and SQL Server 2008.
Device= Note 2
android:minSdkVersion="10"
When I run the code snippet on the UI thread It gives me the error "Unable to get information from SQL Server:", but if I run the code in a background thread or AsyncTask Thread it works. Any help in understanding the two processes would be greatly appreciated?
Note: "The issue occurred when I upgrade the minSDKVersion from 8 to 10."
public Boolean open() {
Boolean res = false;
try {
String s = "jdbc:jtds:sqlserver://" + "xx.xxx.x.xx" + "/" + "FOO" + ";instance=SQLEXPRESS";
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = DriverManager.getConnection(s, "UserFoo", "1234");
res = true;
} catch (SQLException sx) {
Log.e(TAG, "DB:Open - " + sx.toString());
} catch (ClassNotFoundException e) {
Log.e(TAG, "DB:Open - " + e.toString());
}
return res;
}
Starting with Android 3.0, trying to access the network on the main (UI) thread results in:
NetworkOnMainThreadException
I'm guessing that you didn't see it because somewhere higher in your call stack you have:
catch (Exception)
which leads to:
"Unable to get information from SQL Server:"
As you have already discovered, a separate thread is the solution.