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)
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.
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 want to connect my Android app to remote MongoDB database:
try {
String str = "";
MongoClientURI uri = new MongoClientURI("mongodb://byulent:mypass#ds231549.mlab.com:31549/mydb");
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase db = mongoClient.getDatabase(uri.getDatabase());
MongoCollection<Document> photos = db.getCollection("photos");
FindIterable<Document> cursor = photos.find();
Iterator i = cursor.iterator();
while (i.hasNext()){
Log.d("obj", i.next().toString());
}
//Toast.makeText(context, "Succesfully connected to MongoDB", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("mongodb", e.getMessage());
}
But when I try to get data from DB, I see this error:
E/mongodb: Timed out after 30000 ms while waiting to connect. Client view of cluster state
is {type=UNKNOWN, servers=[{address=ds231549.mlab.com:31549, type=UNKNOWN, state=CONNECTING,
exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by
{java.net.SocketTimeoutException: failed to connect to ds231549.mlab.com/34.245.70.193
(port 31549) after 20000ms}}]
Why this error occurs and how to fix it?
I'm trying to query a Pervasive database on my file server which is on a network with several other computers. On the router, I've set the appropriate ports to forward to the file server which is running the PSQL engine.
by visitng ip4.me, I got the ip address of the router.
here's the code:
class Task implements Runnable {
public void run() {
String routerIP ="*.*.*.*"; //blanked it out for privacy reasons
//String url = "jdbc:pervasive://192.168.1.139:1583/DB"; // THIS WORKS LOCALLY
String url = "jdbc:pervasive://" + routerIP + ":1583/DB"; //this throws exception
String query = "select NAME from CUSTOMER";
Statement stmt;
try {
Class.forName("com.pervasive.jdbc.v2.Driver");
} catch (Exception e) {
Log.d("ClassNotFoundException:", e.toString());
toast1.show();
}
try {
Connection conn = DriverManager.getConnection(url, "username", "password");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
int i = 1;
while (rs.next()) {
for (i = 1; i <= numberOfColumns; i++) {
Log.d("Data", rs.getString(i));
myStringArray1.add(rs.getString(i));
}
}
stmt.close();
conn.close();
} catch (SQLException ex) {
Log.d("Error", ex.toString());
toast2.show();
}
}
}
A couple of different things are happening. When I try the connection on the local wifi (same network as the file server), I get java.sql.SQLException: java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
Attempting to use a 4G connection (what I intend to use), I get
java.net.ConnectException: failed to connect to /*.*.*.* (port 1583): connect failed: ETIMEDOUT (Connection timed out)
(again, I blanked out the ip for privacy reasons)
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