Home > Mobile >  Android development, using the JDBC direct connect the mysql database, always fail, ask bosses QWQ s
Android development, using the JDBC direct connect the mysql database, always fail, ask bosses QWQ s

Time:10-20

Newbie on the road, lane for nearly half a day, it is can not find out where is the problem, ask bosses save QWQ

My development environment is:

8.0.11 version of mysql database
Driver: mysql connector - Java - 8.0.11. Jar
Android stdio 3.2


Running, the console has been submitted to the following error:



The directory structure is like this:




In the MainActivity code:

 package com. Example. Database_test; 

The import android. OS. Bundle;
import android.support.v7.app.AppCompatActivity;
The import android. View. The view;
The import android. Widget. The Button;

Public class MainActivity extends AppCompatActivity implements the View. An OnClickListener {

@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Activity_main);

The Button oBtn=(Button) the findViewById (R.i db utton_start);
OBtn. SetOnClickListener (this);

}

@ Override
Public void onClick (View v) {
The switch (v.g etId ()) {
Case R.i db utton_start:
New Thread (new Runnable () {
@ Override
Public void the run () {
Try {
DaoTest. Gets ();
} the catch (Exception e) {
e.printStackTrace();
}
}
}). The start ();
break;
Default:
break;
}
}
}



DaoTest within code:

 package com. Example. Database_test; 


The import android. Util. Log;

import java.sql.Connection;
import java.sql.DriverManager;
Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;
Import Java, SQL Statement;

Public class DaoTest {

Public static void gets () throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
String driver="com.mysql.cj.jdbc.Driver";
The String url="JDBC: mysql://localhost: 3306/day14";
String username="XXX";
String password="yyy";


//1: load the driver
Class.forname (driver). NewInstance ();


//2: getting connection
The Connection conn=DriverManager. GetConnection (url, username, password);



if(! Conn. IsClosed ())
System. Out.println (" succeeded to connect the database!" );

//3: access to database SQL statement object
The Statement st=conn. CreateStatement ();

//4: SQL statements to the database method, obtain the data is encapsulated into a result set (ResultSet)
The ResultSet rs=st. executeQuery (" select * from the users ");

//5: from the result set object, to get the data
While (rs), next ()) {
The d (" id=", (String) rs. GetObject (" id "));
The d (" name=", (String) rs. GetObject (" name "));
The d (" password=", (String) rs. GetObject (" password "));
The d (" email=", (String) rs. GetObject (" email "));
The d (" birthday=", (String) rs. GetObject (" birthday "));
}


//6: release links, and other resources
rs.close();
St. close ();
Conn. Close ();

}

}


In the MainFest, has set a network access for




The couple is really can't find where the problem, who save QWQ genuflect is begged

CodePudding user response:

Supplement:

Build within the app. Grade is like this:

CodePudding user response:

From the top, bosses help QWQ

CodePudding user response:

? JDBC precompiled add and delete

http://www.verejava.com/? Id=1715771977132

CodePudding user response:

Do not use the root login users

CodePudding user response:

The first time I saw the app directly connect to the database, the server, please do eat

CodePudding user response:

Mysql was installed above your cell phone?
Pay attention to the connection address you,

CodePudding user response:

Your CLass. Class.forname perhaps didn't perform successfully, it will not mistake to quote you empty file address?

CodePudding user response:

So long posts was also interpret
  • Related