MongoDB on Android - socket timeout - 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?

Related

java.sql.SQLException: Invalid object name 'tablename' in android studio

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);
}

Connecting to Local SQL Server 2008 from my Application

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)

Android remote JDBC connection

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)

Android - Connecting MS SQL Server 2008 R2 - Error Connection NULL - Eclipse

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

How to connect to MySQL database from Android?

I try to connect to MySQL through mysql-connector-java-5.1.21-bin.jar library.
This is my code:
class Test {
private static final String CONNECTION = "jdbc:mysql://x.x.x.x:3306/db";
private void getMySQL() {
Connection con = null;
String str_res = "";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
// Properties for user and password.
Properties p = new Properties();
p.put("user","***");
p.put("password","***");
// Now try to connect
Connection c = DriverManager.getConnection(CONNECTION, p);
...
} catch (Exception e) {}
}
But I have a network exception:
com.mysql.jdbc.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
java.net.SocketException: The operation timed out
null
null
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
0
0
What's wrong?

Categories

Resources