A complete function of the Java connection database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.