Java Connection MySQL Database

Source: Internet
Author: User

I've shared two small apps I've written myself, see this blog post Baidu Map Development of two application source sharing (Android version), I did not expect someone to find me to do, years ago delivery, time is not very tight, probably understand the next, the main use is and server-side interaction, this I have not done before, so the next time will be updated this some of the columns of the blog, record the drip of learning. The first is Java reading the MySQL database file. Installation and configuration files will be uploaded to my Baidu network for download later. Other introductions on the internet can be found related to the blog, the following direct source code.

MySQL builds a database named "Vge_whu" and creates a new user table in the database. The specific information is as shown in.


Mysqlhelper.java,mysql operation class, followed by the improvement of the class, the source code is as follows:

Package Edu.whu.vge;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.PreparedStatement; Import java.sql.sqlexception;/** * Project name: JavaSQL1 * Class Name: DBHelper * class Description: MySQL database operation class * Creator: Administrator * created on: 2 014-11-25 5:11:11 * Modify NOTE: * @version */public class mysqlhelper{public static final String url = "jdbc:mysql://127 .0.0.1/vge_whu ";   Database connection public static final String name = "Com.mysql.jdbc.Driver";  Program-driven public static final String user = "root"; User name public static final String password = "[email protected]";    Password public Connection conn = null;public PreparedStatement PST = null;/** * * Create a new instance dbhelper. * * @param sql:sql query Statement */public mysqlhelper (String sql) {try{class.forname (name);//Specify connection Type conn = Drivermanager.getconne ction (URL, user, password);//get connection PST = conn.preparestatement (SQL);//Prepare to execute statement} catch (Exception e) {e.printstacktrace ();}} /** * Method Name: Close; * Method Description: Close database connection; * Parameters: * return type: void; * Creator: James; * Created: 2014-11-25 pm 7: 00:12; * @throws */public void Close () {try{this.conn.close (); This.pst.close ();} catch (SQLException e) { E.printstacktrace ();}}}
Write a Java file to invoke the Mysqlhelper class to perform related operations, temporarily only query, and then add new, Delete, update and other operations.

Package Edu.whu.vge;import java.sql.*;/** *  * Project name: JavaSQL1  * class Name: Jdbctest  * Class Description: Java connection MySQL * Test  Created by: Administrator * Created: 2014-11-25 pm 5:11:43 *  Modify NOTES: *  * @version 1.0 */public class jdbctest{static String sql = null;static mysqlhelper db1 = null;static ResultSet ret = null;public static void Main (string[] args) {sql = "SELECT * Fro M user ";//SQL statement DB1 = new mysqlhelper (SQL);//Create DBHelper object System.out.println (" Number--name--sex--age-------phone-------QQ---------mailbox "); Try{ret = Db1.pst.executeQuery ();//execute statement, get result set while (Ret.next ()) { String uId = ret.getstring (1); String uName = ret.getstring (2); String usex = ret.getstring (3); String uage = ret.getstring (4); String Utel = ret.getstring (5); String uqq = ret.getstring (6); String Umail = ret.getstring (7); System.out.println (uId + "\ T" + UName + "\ T" + Usex + "\ T" + uage + "\ T" + Utel + "\ T" + uqq + "T" + Umail);} Display Data ret.close ();d b1.close ();//Close connection} catch (SQLException e) {e.printstacktrace ();}}}
The execution results are as shown.



Welcome to Message exchange.


Java Connection MySQL Database

Related Article

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.