Home > Mobile >  Small white to ask: unable to connect to the database in fragments. Please answer
Small white to ask: unable to connect to the database in fragments. Please answer

Time:10-16

I am a rookie, released to the great god help:
Himself wrote a link to the mysql database DButils classes, can link smoothly in the Activity, but Cannot be connected in fragments, debugging shows that connection is empty, and display (MySQLNonTransientConnectionException) (e). SQLState=always find a local variable 'e', the code is as follows, please help!

DButils classes:

Package com. Example. The asus. Wechat;

The import android. Util. Log;
Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import of Java, SQL PreparedStatement;
Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;
Import Java, SQL Statement;
Import the Java. Util. ArrayList;
import java.util.HashMap;
Import the Java. Util. List;

/* *
* database tools: connect to the database, access to the database data
* related database operation method are written in the book the class
*/
Public class DBUtils {

Public static Connection Connection=null;
Private static String driver=". Com. Mysql. JDBC driver ".//MySql driver
Private static PreparedStatement PDS.

Private static String user="root";//user name
Private static String password="888888";//password
/* *
* connect to the database
* */

Public static Connection getConn (String dbName) {
The connection=null;
Try {
Class.forName(driver);//dynamic loading class
String IP="10.0.2.2";//write 10.0.2.2, cannot be written as localhost, because mobile IP is loaclhost
//try to establish the connection to the given database URL
The connection=DriverManager. GetConnection (" JDBC: mysql://"+ IP +" : 3306/+ dbName + "?" useUnicode=true& CharacterEncoding=utf-8 & amp; UseSSL=false& AutoReconnect=true& FailOverReadOnly=false, "
The user, password);
} the catch (Exception e) {
e.printStackTrace();
}
return connection;
}

/* *
* the query
* */

Public static HashMap GetInfoByName (String name) {

HashMap The map=new HashMap<> (a);
//, according to the name of the database connection is established
Dblearner Connection Connection=getConn (" ");

Try {
//mysql simple query, here is based on MD_CHARGER table NAME field to query a record
String SQL="select * from uase where name=?" ;
//String SQL="select * from MD_CHARGER";
if (connection !=null) {//established connection with the database connection is not null said
PreparedStatement ps=connection. PrepareStatement (SQL);
If (ps!=null) {
//set in the above SQL statement? The value is the name of the
Ps. SetString (1, name);
//execute SQL query and returns a result set
The ResultSet rs=ps. ExecuteQuery ();
If (rs!=null) {
Int count=rs. For getMetaData (.) getColumnCount ();
The e (" DBUtils ", "total number of columns:" + count);
While (rs), next ()) {
//note: subscript begins with 1
For (int I=1; i <=count; I++) {
String field=rs. For getMetaData (.) getColumnName (I);
The map. The put (field, rs. Get string (field));
}
}
Connection. The close ();
Ps. The close ();
return map;
} else {
return null;
}
} else {
Return null;
}
} else {
Return null;
}
} the catch (Exception e) {
e.printStackTrace();
The e (" DBUtils ", "exception:" + um participant etMessage ());
return null;
}

}

}


Fragment_11. Java

Package com. Example. The asus. Wechat;

The import android. The annotation. SuppressLint;
The import android. App. Person;
The import android. The content. The Context;
The import android.net.Uri;
The import android. OS. Bundle;
The import android. OS. Handler;
The import android. OS. The Message;
The import android. Support. The v4. App. Fragments;
The import android. Util. Log;
The import android. View. LayoutInflater;
The import android. View. The view;
The import android. View. ViewGroup;
The import android. Widget. The Button;
The import android. Widget. The EditText;
The import android. Widget. ListView.
The import android. Widget. SimpleAdapter;
The import android. Widget. TextView;

Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import of Java, SQL PreparedStatement;
Import the Java. SQL. The ResultSet;
Import the Java. Util. ArrayList;
import java.util.HashMap;
Import the Java. Util. List;
Import the Java. Util. The Map;



Public class fragment_11 extends fragments {

Private static final String ARG_PARAM1="param1";
Private static final String ARG_PARAM2="param2";

Private SimpleAdapter mAdpater;
Private Button btn_get_data;
Private TextView tv_data;
Private EditText EditText;
Private Button ButAdd;
Private Button ButSelect;
Private EditText editid;
Private EditText editname;
Private EditText editpassword;
Private EditText editsex;
Private EditText editphote;
Private Button ButDel;
Private ListView lv.
Private Button ButQue;
Private Button ButNext;
Private List Lists;
Private List Data=https://bbs.csdn.net/topics/new ArrayList ();

//TODO: Rename and change the types of the parameters
Private String mParam1;
Private String mParam2;

Private OnFragmentInteractionListener mListener;


@ SuppressLint (" HandlerLeak ")
Private Handler Handler=new Handler () {
@ Override
Public void handleMessage (Message MSG) {

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related