Java Connection MySQL Database

Source: Internet
Author: User

1. Install MySQL First

2. Second easy to operate MySQL should download a graphical interface to the database operating software: for example mysql-front or EMS SQL Manager

3. Install the EMS will prompt to set the root user's password, this password will be used to connect to the database later. For example, set to "126456"

4. Second, you want to compile the function containing the database operation to include the Mysql.jar in the JCreator
Configuration-Options-JDK profile-JDK version-edit-add archive, select Mysql.jar.

The operation of the database cannot contain two aspects: Load driver + Get Connection object, then all operations are based on connection object. Give the detailed steps for the database connected to MySQL:

Import java.sql.*;

public class RR
{
public static void Main (string[] args)
{
Try
{
String url= "Jdbc:mysql://localhost/mydb";
String user= "root";
String pwd= "123456";

Load driver, this sentence can also be written as: Class.forName ("Com.mysql.jdbc.Driver");
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
Establish a connection to MySQL
Connection conn = drivermanager.getconnection (Url,user, PWD);

Execute SQL statement
Statement stmt = Conn.createstatement ();//Create statement object to execute SQL language
ResultSet rs = stmt.executequery ("SELECT * from student");

Working with result sets
while (Rs.next ())
{
String name = rs.getstring ("name");
SYSTEM.OUT.PRINTLN (name);
}
Rs.close ();//Close Database
Conn.close ();
}
catch (Exception ex)
{
System.out.println ("Error:" + ex.tostring ());
}
}
}

Java Connection MySQL 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.