Android Communications link failure when minSdkVersion is greater than 9 - java

Using the mysql-connector-java.5.1.26-bin.jar to connect my android app to a remote mysql host. Problem is, anything above minSdkVersion 9 casuses the "Communications link failure". If I have it set to 9, it connects fine and gets database information. Here's my connection code:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from users");
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()) {
....
}
}
catch(Exception e) {
e.printStackTrace();
}
Any ideas as to why this is happening?

Turns out the problem was that I was trying to do a network connection in the main activity. For those that might have the same problem, look into using asynctask to complete the connection.
http://developer.android.com/reference/android/os/AsyncTask.html

Here is my Development Environment:
OS: Ubuntu14.04
IDE: Eclipse Luna
DB: MySQL5.1.73
Firstly,I put the code below in OnCreate method:
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, username,
password);
Statement stmt = conn.createStatement();
String sql = "select distinct Action from attributes";
ResultSet rs = stmt.executeQuery(sql);
The connection code is OK, But It will occur:
communication link failure.The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
So it must be wrong in something else. After trying various method, and solve it using android AsyncTask. Just put the code relative to operating mysql in Class that extends AsyncTask.
Below is my code:
private class Connect extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... urls) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, username,
password);
Statement stmt = conn.createStatement();
String sql = "select distinct Action from attributes";
ResultSet rs = stmt.executeQuery(sql);
List<String> actions = new ArrayList<String>();
while (rs.next()) {
actions.add(rs.getString("Action"));
}
} catch (Exception e) {
Log.d("MySQLConnection", e.getMessage());
}
return null;
}
}

Related

How to set up online database for java netbeans application

i'm trying a online database connection for my java application, but it seems my database isn't connected well this is my java code :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("sql101.epizy.com/epiz_26206530_sekolah","LOGIN","PASSWORD");
String query = "select * from users where username=? and password=?";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, jTextField1.getText());
pst.setString(2, jPasswordField1.getText());
ResultSet rs = pst.executeQuery();
if(rs.next()) {
JOptionPane.showMessageDialog(null, "Login Berhasil");
jLabel7.setText(rs.getString(1));
welcome well = new welcome();
this.setVisible(false);
well.setVisible(true);
}
} catch (Exception e) {
}
}
i already input MySQL JDBC Driver library, by the way i'm using online database from https://infinityfree.net/

What is a proper way to connect to a sqlite database, execute a prepared Statement and close the connection with java?

This is my code at the moment. I have or had several problems:
Database is busy - error
SQL error or missing database
Resultset closed
Which code structure should I use if I want to prevent both of these errors?
Do I need to connect in the try block? Where do I need to close the connection? Where do I need to close the preparedStatement?
I was not able to find a solution by connecting the information of different pages by myself until now.
public boolean checkForUser(String username){
try(Connection con = this.connect();
PreparedStatement pstmt = createPreparedStatementRegistrate(conn, username);
ResultSet rs= pstmt.executeQuery();)
{
if (rueck.next()){
pstmt.close();
rs.close();
con.close();
//do some stuff
}
}catch(SQLException e){
e.printStackTrace();
}
return null;
}
private PreparedStatement createPreparedStatementRegistrate(Connection conn, String username) throws SQLException {
String sql = "SELECT * FROM users WHERE username = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
return pstmt;
}
private Connection connect() {
String url = "jdbc:sqlite:user.db";
Connection con = null;
try {
con = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
java.sql.SQLException: ResultSet closed
at org.sqlite.core.CoreResultSet.checkOpen(CoreResultSet.java:69)
at org.sqlite.jdbc3.JDBC3ResultSet.findColumn(JDBC3ResultSet.java:38)
at org.sqlite.jdbc3.JDBC3ResultSet.getString(JDBC3ResultSet.java:443)
at zugriffsverwaltung.Zugriffsverwaltung.loeschen(Zugriffsverwaltung.java:215)
at zugriffsverwaltung.Zugriffsverwaltung.<init>(Zugriffsverwaltung.java:37)
at main.Server.<init>(Server.java:29)
at main.Server.main(Server.java:46)
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (Connection is closed)
at org.sqlite.core.DB.newSQLException(DB.java:909)
at org.sqlite.core.CoreStatement.internalClose(CoreStatement.java:115)
at org.sqlite.jdbc3.JDBC3Statement.close(JDBC3Statement.java:35)
at org.sqlite.jdbc4.JDBC4Statement.close(JDBC4Statement.java:27)
at zugriffsverwaltung.Zugriffsverwaltung.loeschen(Zugriffsverwaltung.java:238)
at zugriffsverwaltung.Zugriffsverwaltung.<init>(Zugriffsverwaltung.java:37)
at main.Server.<init>(Server.java:29)
at main.Server.main(Server.java:46)
The problem is that you are closing the result set, statement and connection before you do things with the result set. This is not allowed: a result set needs to be open when you retrieve values using getString (not shown in your code, but present in your stack trace).
As you are using try-with-resource which will close the resource on exit, it is even unnecessary to explicitly close. It also looks like the SQLite driver does not meet the JDBC requirements that closing an already closed resource should be a no-op, as it is throwing an exception that the connection is closed when closing the statement (likely by the try-with-resources).
You need to change your code to
public boolean checkForUser(String username){
try(Connection con = this.connect();
PreparedStatement pstmt = createPreparedStatementRegistrate(conn, username);
ResultSet rs= pstmt.executeQuery();)
{
if (rueck.next()){
//do some stuff
}
}catch(SQLException e){
e.printStackTrace();
}
return null;
}
That is, remove the lines
pstmt.close();
rs.close();
con.close();

JAVA: executing Catch not try

private void rtrBtnActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) depTbl.getModel();
try{
Class.forName("java.sql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test1","admin","root");
Statement stmt = con.createStatement();
String query = "SELECT * FROM dept;";
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
String dno = rs.getString("deptno");
String dName = rs.getString("dname");
String lc = rs.getString("loc");
model.addRow(new Object[] {dno,dName,lc});
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error In Connectivity");
}
}
Im trying to connect my JForm to the mysql database but not able to connect to the database, continuously executing catch statement "Error in Connectivity", please help how should i resolve this issue..............................................................................
Change the following line
Class.forName("java.sql.Driver");
to
Class.forName("com.mysql.jdbc.Driver");
in your code
This line of code is not correct - Class.forName("java.sql.Driver");
The java.sql.Driver is an interface. You need to provide the correct jdbc driver class for you appropriate db.
Such as for Oracle - Class.forName("oracle.jdbc.driver.OracleDriver");

How to access to NexusDB from Java

I want to access to NexusDB V3 with Java.
So I have a Java project with many files that connects to the database. Can anyone tell me if it is possible to use a Java class file for connecting to the database.
I've tried JDBC connector and the ODBC connector but nothing is working.
I have read about a bridge between them but I don't know how to make it so please help.
public class dbConnect {
public static void connect(){
Connection conn;
Statement stmt;
ResultSet rs;
String sql;
conn = null;
String url = "jdbc:mysql://localhost:3306/db_oopproject";
try{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url,"user","12345");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
sql = "Select * from user_account";
rs = stmt.executeQuery(sql);
}
catch (Exception e){
System.out.print(e.getMessage());
}
}
}
first off, your url contains "mysql", are you sure that will work for the jdbc bridge to connect to NexusDB?

MYSQL select gets deleted values (JAVA)

I have a mysql database on my ubuntu server. When i delete rows with MYSQL workbench on this database and than try to select these rows with my Java program, i still get the deleted rows. (I have committed the command with MYSQL workbench)
In my java program I access the database over a Java Restful webservice which also runs on my server.
my webservice looks like:
#Path("/getAll")
#GET
#Produces({MediaType.APPLICATION_XML})
public List<PV> getAllPV() {
List<PV> ret =null;
try {
ret=Database.getInstance().getAllPV();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
My Database:
public List<PV> getAllPV() throws Exception{
Connection conn = null;
PreparedStatement ps=null;
Statement s = null;
conn = getDBConnection();
conn.setAutoCommit(false);
List<PV> ret = new ArrayList<PV>();
s = conn.createStatement();
ResultSet rs = s.executeQuery("select * from PV");
...
conn.close();
}
Has anyone an idea ?

Categories