Java Link MySQL operation

Source: Internet
Author: User

Java link MySQL database operationJava link mysql first loads MySQL jdbc driver in Java

Class.forName ("Com.mysql.jdbc.Driver");

Second, establish a connection with MySQL existing database

String url = "Jdbc:mysql://localhost:3306/mydata?userunicode=true&characterencoding=utf-8&usessl=false"

Where JDBC is called the JDBC driver, MySQL refers to the database type here is the MySQL database, localhost refers to the link address, here refers to the local MySQL database, 3306 is the port number used by MySQL,useunicode=true& Characterencoding=utf-8 is the encoding format that is used when displaying database data, Usessl=false the SSL link is off, the SSL connection must be established in the higher version, or an error will be made.

Strinf user = "root"; String psaawd = "123456"

User is the MySQL username, passwd is the password

Connection con = drivermanager.getconnection (URL,USER,PASSWD);

As part of the initialization, the DriverManager class attempts to load the driver class that is referenced in the "Jdbc.drivers" system property
getconnection (string url, string user, String password) method attempts to establish a connection to a given database URL


Since then, a link has been established with the database

public static Connection getconnection () throws sqlexception,java.lang.classnotfoundexception{// Load the MySQL JDBC driver class.forname ("Com.mysql.jdbc.Driver");//Get the URL and user name and password of the connection string url = "jdbc:mysql://localhost:3306/ Mydate?useunicode=true&characterencoding=utf-8&usessl=false "; String user = "root"; String passwd = "123456";//Create an instance connection to the database connection con = drivermanager.getconnection (URL, user,passwd); return con;}

  

Java Link MySQL operation

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.