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
Related
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);
}
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
so I have an app that is running and on startup, I would like to be able to Get the IP address and display it as a String. I have been using the code below.
String ipAddress = "";
try{
ipAddress = Inet4Address.getLocalHost().getHostAddress();
}
catch(Exception e){
ipAddress = "IP address Cant be used";
}
every time this is run it will return "IP address Cant be used" so it's throwing an error.
If you are looking to get your public facing IP check out this answer. In short you cannot get your public facing IP because the Network Address Transation does not happen in your Kernel, i.e you dont assign your IP to yourself rather it will be given to you thanks to NAT and DHCP. The following code makes a request to amazons's aws IP API, to retrieve your IP
public static String getIp() throws Exception {
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(
whatismyip.openStream()));
String ip = in.readLine();
return ip;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
When I connect to MySQL in android:
code show as below
protected static void connMysql(){
Connection conn = null;
PreparedStatement pstm = null;
ResultSet res = null;
String openurl_mysql="jdbc:mysql://10.15.26.21:3306/etrack_user&autoReconnect=true&failOverReadOnly=false";
try {
java.sql.Driver.class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(openurl_mysql,"etrack","_etrack_mysql_");
pstm = conn.prepareStatement("select count(*) as count from et_patrol_task");
res = pstm.executeQuery();
while(res.next()){
int count = res.getInt("count");
System.out.println("return success=============="+count);
}
res.close();
pstm.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
When I run the program,I get the following problem:enter image description here
Has anyone encountered a similar problem or solved it?
You can't access a MySQL DB from Android natively.Actually you may be able to use JDBC, but it is not recommended.
JDBC is infrequently used on Android, and I certainly would not recommend it.
IMHO, JDBC is designed for high-bandwidth, low-latency, highly-reliable network connections (e.g., desktop to database server, Web application server to database server). Mobile devices offer little of these, and none of them consistently.
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