How to access the data in the mysql database in a Java program and perform simple operations, javamysql

Source: Internet
Author: User

How to access the data in the mysql database in a Java program and perform simple operations, javamysql

In the previous article, I introduced how to connect Myeclipse to the mysql database. Through this article, I will introduce how to access the data in the mysql database in Java programs and perform simple operations, for more information, see the following section.

Create a javaProject and enter the following java code:

Package link; import java. SQL. *;/*** process of connecting to MySQL using JDBC * DataBase: fuck, table: person; * basic operations for adding, deleting, modifying, and querying mysql databases using myeclipse. */Public class JDBCTest {public static Connection getConnection () throws SQLException, java. lang. classNotFoundException {// Step 1: load the JDBC Driver Class of MySQL. forName ("com. mysql. jdbc. driver "); // get the connection url, the user name and password that can access the MySQL database; jsj: Database Name String url =" jdbc: mysql: // localhost:/fuck "; string username = "root"; String password = ""; // Step 2: Create an instance Connection con = DriverManager for the Connection class to the MySQL database. getConnection (url, username, password); return con;} public static void main (String args []) {try {// Step 3: Get the connection class instance con, use con to create a Statement object class instance SQL _statement Connection con = getConnection (); Statement SQL _statement = con. createStatement (); // if a database with the same name exists, delete // SQL _statement.executeUpdate ("drop table if exists student "); // execute an SQL statement to generate a table named student // SQL _statement.executeUpdate ("create table student (id int not null auto_increment, name varchar () not null default 'name ', math int not null default, primary key (id); "); // insert data into the person table SQL _statement.executeUpdate (" insert person values (, 'liying ',)"); values ("insert person values (, 'jiangshance',)"); SQL _statement.executeUpdate ("insert person values (, 'wangjiawu ',)"); SQL _statement.executeUpdate ("insert person values (, 'duchangfeng',) "); // Step 4: Execute the query. Use the ResultSet class object to return the query result String query =" select * from person "; resultSet result = SQL _statement.executeQuery (query); // display the content of the person table in the data: System. out. println ("data in the person table is as follows:"); System. out. println ("------------------------"); System. out. println ("no." + "" + "name" + "+" score "); System. out. println ("------------------------"); // process the obtained query results and operate on the objects of the Result class while (result. next () {int number = result. getInt ("number"); String name = result. getString ("name"); String mathsorce = result. getString ("mathsorce"); // obtain the data System in the database. out. println ("" + number + "" + name + "" + mathsorce);} // close the connection and declare SQL _statement.close (); con. close ();} catch (java. lang. classNotFoundException e) {System. err. print ("ClassNotFoundException"); System. err. println (e. getMessage ();} catch (SQLException ex) {System. err. println ("SQLException:" + ex. getMessage ());}}}

Note that you need to modify the settings.

For example, the url and account, and password must be consistent with your own.

The data to be accessed must match the type in the database to print the correct result.

Right-click the project name --> Build Path --> Configure bicould Path --> Libraries --> Add External JARs --> Add a jdbc package (For details, refer to Mysql's simple use (1 )) ---> OK

In this case, a Referenced Libraries package file is added to the package, indicating that the configuration is successful.

Click Run as ---> Run Java Application ---> JDBCTest -- link --->. The result is as follows:

The above section describes how to access data in the mysql database and perform simple operations in Java programs. I hope this will help you, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

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.