Home > Back-end >  A JDBC connection database error
A JDBC connection database error

Time:04-06

Android studio to connect mysql error, your user name and password yes, Navicat on operation success can also link,
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 better

Estimation 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:

reference 1st floor h2plus0 response:
this is the Android applications, suggest or go to the mobile development & gt;> Android plate better

Estimation 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,,,

This is the Android applications, suggest or go to the mobile development & gt;> Android plate better

Estimation 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,,,


Oh I'm sorry, this I do not know much about plate randomly choose the don't know can change now... And then I mysql is built in the computer, the mobile phone should not connect to the database?

CodePudding user response:

Even though android is also a Java programming, but believes it is android plate more accurate, professional reply,
May not change this time,,,

In the connection string localhost corresponds to the device - & gt; Mobile phones, is equivalent to the database to connect to a mobile phone, rather than on a computer database,
If you want to connect the database on the computer, should be used, the computer's IP
With Navicat can connect because on computer, computer, localhost corresponds to the device, is a computer,

In short, localhost for this device IP address
  • Related