The name of the column to query for the first parameter
The second parameter is the URL of the connection
The third parameter is a user name
Fourth parameter password
The fifth parameter is the command to execute.
Note that the URL format is
Jdbc:mysql://localhost:3306/wechat
JDBC: is unchanged, I use Musql, so, Jdbc:mysql//,localhost is the machine, if you have a link, fill the connection
3306 is the port of the database,/wechat here is its own database name
Before using, two-dimensional string arrays are defined first, as well as some other variables.
1 Publicstring[][] Init_driver_and_connect (String col_name,string url,string user,string pwd,string order) {2 intI=0;3 Try {4Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();//Initialize driver5conn = drivermanager.getconnection (URL, user, pwd);//connecting to a database6Statement stmt = Conn.createstatement ();//Create statement object to execute SQL language7rs = stmt.executequery (order);//The table and command execution to query8Rs.last ();//Traverse9Row_sum=rs.getrow ();//number of rows returnedTenData_adapter=NewSTRING[ROW_SUM][100];//initializes the size of a two-dimensional string array One while(Rs.next ()) {//get the value one by one and enter it into a two-dimensional array AString name = rs.getstring (col_name);//all columns that get values -data_adapter[i][100]=name; -i++; the } -Rs.close ();//Close the database - conn.close (); -}Catch(Exception e) { +SYSTEM.OUT.PRINTLN ("Connecting the DB Fauil"); - } + returnData_adapter;//return A}
A complete function of the Java connection database