Baidu to all tried all useless... The ball ball old man look
Code:
Public class MainActivity extends AppCompatActivity implements the View. An OnClickListener {
Private static final String TAG=MainActivity. Class. GetSimpleName ();
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MButton=(Button) the findViewById (R.i d.s qltestb);
MButton. SetOnClickListener (this);
}
@ Override
Public void onClick (View v) {
The switch (v.g etId ()) {
Case R.i d.s qltestb:
Final Thread Thread=new Thread (new Runnable () {
@ Override
Public void the run () {
//1. Load the JDBC driver
Try {
Class.forName("com.mysql.jdbc.Driver");
The v (TAG, "load the JDBC drive success");
} the catch (a ClassNotFoundException e) {
The e (TAG, "load the JDBC driver failed");
return;
}
Final String DBURL="JDBC: mysql://localhost: 3306/testtb";
Final String DBUSER="root";
Final String DBPASSWORD="* * * * * *";
String r=null;
Try {
Connection c=DriverManager. GetConnection (DBURL, DBUSER, DBPASSWORD);
The d (TAG, "Mysql connection success");
String SQL="select test_id from test_tb";
PreparedStatement PST=c.p repareStatement (SQL);
The ResultSet rs=PST. ExecuteQuery ();
{if (rs. Next ())
R=rs. Get string (1);
}
Rs. The close ();
PST. The close ();
Arthur c. lose ();
} the catch (SQLException e) {
The e (TAG, "Mysql connection failure" + e. oString ());
e.printStackTrace();
}
Error:
V/MainActivity: load the JDBC driver success
E/MainActivity: Mysql connection com.mysql.jdbc.exceptions.jdbc4.Com municationsException: Communications link failure
The last packet sent successfully to The server was 0 milliseconds line. The driver has not received any packets from The server.
W/System. Err: com.mysql.jdbc.exceptions.jdbc4.Com municationsException: Communications link failure
W/System. Err: The last packet sent successfully to The server was 0 milliseconds line. The driver has not received any packets from The server.
W/System. Err: at Java lang. Reflect. Constructor. NewInstance0 (Native Method)
The at Java. Lang. Reflect. Constructor. NewInstance (343) Constructor. Java:
At com. Mysql. JDBC. Util. HandleNewInstance (Util. Java: 404)
At com. Mysql. JDBC. SQLError. CreateCommunicationsException (SQLError. Java: 988)
CodePudding user response:
This is the Android applications, suggest or go to the mobile development & gt;> Android plate betterEstimation is the problem of the connection string:
Final String DBURL="JDBC: mysql://localhost: 3306/testtb";
This is equivalent to connecting database on the mobile phone, need to change the database server's IP,,,
CodePudding user response: