PHP MySQL and Android connection - java

I'm trying to connect my android app with an online database server that uses MySQL and phpMyAdmin.
But problem I am having is that I'm unable to access my database (which is online) using Java code for android.
It get the following error:
java.sql.SQLException: No suitable driver at java.sql.DriverManager.getConnection(DriverManager.java:187)
I already loaded the driver and external JAR of MySQL but it is still showing this exception.
The code is :
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql:http://www.demo.com:2082/",
"usernme", "password"); //username and password of cpanel access
PreparedStatement statement = con
.prepareStatement("SELECT * FROM users WHERE userName = '"
+ userName + "'");
ResultSet result = statement.executeQuery();
while (result.next()) {
retrievedUserName = result.getString("username");
Log.e("data username:", retrievedUserName);
retrievedPassword = result.getString("password");
}

Use the Webservice to make the Connection to Mysql from your android app through json or xml.you can use SQlite database to connect the android app internally by the use of this package android.database.sqlite classes

Related

Using eclipse to call database from Google Cloud SQL

I m trying to create a dynamic website where I can get data from the database and output it to the user.
I created the connection between MySQL workbench to Google cloud app engine, and I run on the cloud shell to test the database, it works.
But when I try to change the URL on eclipse, it just cannot connect to the server, and gives me errors "Failed to load resource: the server responded with a status of 500 ()".
the URL sting are:
String url = "jdbc:mysql:///film?cloudSqlInstance="
+ "causal-relic-333309:europe-west6:jiayaodb="
+ "&socketFactory = com.google.cloud.sql.mysql.SocketFactory"
+ "&user=root"
+ "&password=password";
private Connection openConnection(){
// loading jdbc driver for mysql
try{
Class.forName("com.mysql.jdbc.Driver");
} catch(Exception e) { System.out.println(e); }
// connecting to database
try{
// connection string for demos database, username demos, password demos
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
} catch(SQLException se) { System.out.println(se); }
return conn;
}
So I think is the connection error where I can't get it to connect to Google Cloud SQL.
you need to use Cloud SQL proxy to connect to the database from your local machine.

What is a best way to give connection string for derby database?

I am trying to connect derby database and inserting some value to the table.for that i use the bellow code.
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=DriverManager.getConnection(
"jdbc:derby://localhost:1527/raptor;create=true","root","root");
if (con != null) {
System.out.println("Connected to database - mydb");
}
Statement st=con.createStatement();
String q="insert into VINOTH values(7)";
st.executeUpdate(q);
In the above code when calling getConnection method I give database user "root" and password as "root", but it gives me the error
Schema 'ROOT' does not exist
Actually root was a database user but it takes as a schema. Why is this happening?
If I give "APP" instead of database user "root" it is working fine. But I need to know why it doesn't take my user and password.

Java MySql Connection Through DSN

I am trying to connect to one of my MySql Databases through a System DSN I set up. The DSN is set up correctly with my SSL certs, username, password, port, and the databases populate the DSN database drop down and the "Test" connection passes. I can't seem to get a connection in Java. I have spent 2 days looking through some examples on Stack but they all refer to an Access database and using JDBC-ODBC bridge which is no longer available in Java 8. I tried using UCanAccess with Jackcess but I have gotten no where. The code below is what I have been tinkering with the last few hours. I normally connect to MySql databases with PHP and receive result in JSON or directly with JDBC driver but for this project neither are really an option. Any ideas. I appreciate the help.
//String username = "<username>";
//String password = "<password>";
//String database = "<database_name>";
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//Connect to cllients MySql Database
Connection conn = DriverManager.getConnection("jdbc:ucanaccess:" + database);
//Call VerifyLabel(<MAC>,<MODEL>); Call provided client
CallableStatement cStmt = conn.prepareCall("{CALL verify(?, ?)}");
//MAC
cStmt.setString(1, "mac address");
//model
cStmt.setString(2, "model");
cStmt.execute();
//Getting results from "Status" column
ResultSet rs1 = cStmt.getResultSet();
//Iterate results and print.
while (rs1.next()) {
System.out.println(rs1.getString("Status"));
}
//Close connection conn
rs1.close();
} catch (SQLException ex) {
Logger.getLogger(CambiumStoredTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CambiumStoredTest.class.getName()).log(Level.SEVERE, null, ex);
}
Using MySql Driver:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:"+ database);
also tried:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+ database);
Error for MySql Driver:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
1) DSN is most commonly assocatiated with ODBC (and often with MS-Access). Hence all the links. ODBC is NOT required for a DSN.
2) Do NOT use Ucanaccess. Use J/Connector for mySQL.
3) Make sure you can communicate with mySQL from the command line. Then focus on getting a small "hello world" JDBC app to connect. Your second and third examples look OK. Be sure to check the mySQL logs for any warnings/errors.
Well, after an entire day of trying to get this to work and sleeping on it for a couple hours I finally got it to work. UCanAccess and mysql-connector did not work. The easiest thing since no other method of connecting to this clients database was acceptable was to push this application in Java 7 rather than 8. This allowed me to Coo=nnect to my DSN with no problems. I understand that this method is not the best solution but it is what is working flawlessly and efficiently. Also, instead of using some rigged up 3rd party libs and jars, I am able to use Connector/J. Thanks everyone for trying to help me. Just incase anyone else runs into this issue, this is how I made it work.
Develope app in Java 7 - not 8.
Set Up System DSN
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//You do not need to provide username or password if it is setup in DSN
Connection conn = DriverManager.getConnection("jdbc:odbc:"+ database);

Unable to fetch data from table of mdb file using JDBC in Java

I am trying to fetch the data from Table of .mdb file. I am able to connect to the file but when i execute the query I get the following error.
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Record(s) cannot be read; no read permission on
However, if I open the .mdb file directly with same user login I can view/edit the table.
Following is the code which I am writing,
conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement();
String selTable = "SELECT * FROM myTable";
ResultSet rs = s.executeQuery(selTable);
What I am doing wrong?
Thanks in advance.
If your access database hasn't any username you can get connection without it. Can you try this:
conn = DriverManager.getConnection(database);
If you are prompted for a username and password when you open the database in Access then the database has "user-level security" enabled. If that is the case then you should supply the username and password of an appropriate Access user as part of the connection string. For some examples, look here.

Receiving SQLException "Login failed for user" connecting to SQL Server 2008

I am trying to connect to SQL Server 2008 via Java.
I've added sqljdbc4.jar to my project's library.
No username and password is set for database accessing the database (Windows Authentication).
The 1433 port is Listening, but I still receive this exception:
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. ClientConnectionId:085d5df3-ad69-49e1-ba32-b2b990c16a69
Relevant code:
public class DataBases
{
private Connection link;
private java.sql.Statement stmt;
public ResultSet rs;
public DataBases()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB;";
Connection con = DriverManager.getConnection(connectionUrl);
}
catch (SQLException e)
{
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE)
{
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
}
If you want windows authentication you need to add the option integratedSecurity=true to your JDBC URL:
jdbc:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true
You also need sqljdbc_auth.dll (beware of 32/64 bit) in your Windows system path or in a directory defined through java.library.path
For details see the driver's manual: http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated
I was having same issue when I tried to connect to Microsoft SQL server from Java. I used jTDS driver instead of regular SQLJdbdc Driver.
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String connectionUrl = "jdbc:jtds:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl);
I had the same issue. Its because of the wrong format of the ConnectionUrl. You are missing username and password in the ConnectionUrl.
String ConnectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB","username","password");"
I hope it works well!!!
This post my help:
jdbc.SQLServerException: Login failed for user for any user
You need the integratedSecurity=true flag and make sure the server properties are indeed set to 'Windows Authentication Mode' under Security.

Categories

Resources