How to access data in a Mysql database in a Java program and do simple operations _mysql

Source: Internet
Author: User
Tags getmessage

In the previous article to introduce the MyEclipse connection MySQL database method, through this article to introduce how to access the MySQL database in the Java program data and simple operation, details please see below.

Create a Javaproject and enter the following Java code:

 Package link;
 Import java.sql.*;
 /** * The process of using JDBC to connect to database MySQL * database:fuck, Table:person; * Use MyEclipse to the MySQL database for the basic operation of the search. */public class Jdbctest {public static Connection getconnection () throws SQLException, Java.lang.ClassNotFoundExcep
  tion {//First step: Loading MySQL's JDBC driver class.forname ("Com.mysql.jdbc.Driver");
  Get the URL of the connection to access the MySQL database username, password; JSJ: database name String URL = "Jdbc:mysql://localhost:/fuck";
  String username = "root";
  String password = "";
  Step Two: Create an instance of the connection class with the MySQL database Connection con = drivermanager.getconnection (URL, username, password);
  return con; public static void Main (String args[]) {try {//Step three: Get connection class instance con, create statement object class instance with Con sql_statement Connection
  con = getconnection ();
  Statement sql_statement = Con.createstatement ();
  If a database exists with the same name, delete//sql_statement.executeupdate ("drop table if exists student"); An SQL statement was executed to generate a table named student//sql_statement.executeupdate ("CREATE TABLE student" (ID int not NULL auto_increment, name V Archar () not Null default ' name ', math int not NULL default, primary key (ID);
  ");
  Inserts a data sql_statement.executeupdate into the person table ("Insert person values (, ' liying ',)");
  Sql_statement.executeupdate ("Insert person values (, ' Jiangshan ',)");
  Sql_statement.executeupdate ("Insert person values (, ' Wangjiawu ',)");
  Sql_statement.executeupdate ("Insert person values (, ' Duchangfeng ',)");
  Step Fourth: Execute the query, using the object of the ResultSet class, returns the query's result String query = "SELECT * from person";
  ResultSet result = sql_statement.executequery (query);
  Displays the contents of the person table in the data: System.out.println ("The data in the person table is as follows:");
  System.out.println ("------------------------");
  SYSTEM.OUT.PRINTLN ("Serial number" + "" + "name" + "" + "Score");
  System.out.println ("------------------------");
  Processing the resulting query results and manipulating the object of the result class while (Result.next ()) {int number = Result.getint ("number");
  String name = result.getstring ("name");
  String Mathsorce = result.getstring ("Mathsorce"); Get the data in the database System.out.println ("" + number + "" + name + "" + Mathsorce);
  }//close connection and declaration 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 ()); }
  }
  }

Notice that there are a few places you need to change.

The URL and account number in the following figure, the password needs to be consistent with your own.

The data that needs to be accessed must match the types in the database to print out the correct results.

Right-click Project name-->build path-->configure biuld path-->libraries--> Add External JARs--> Add a JDBC Package (please refer to the simple use of MySQL (i))--->ok

At this point, a referenced libraries package file will be taken under the package, indicating that the configuration has been successful.

Click Run as---> Run java application--->jdbctest--link---> Display the results as follows:

The above is a small set for you to introduce how to access the MySQL database in the Java program data and simple operation of the relevant knowledge, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.