Java Connection Database

Source: Internet
Author: User
Tags stmt

The Java environment is:

jdk1.8 java Version "1.8.0_171"

1. Installing the MySQL Jar

: http://dev.mysql.com/downloads/connector/j/

When I downloaded it, I couldn't open it, and the blue Light opened quickly. Blue lights are sometimes not available.

Supported versions of JAR:

After the download is complete, Eclipse is required to add the jar package: You can put the jar package into the project

2. Create a database:

CREATE TABLE' student ' (' ID ')int( One) not NULL, ' name 'varchar(255)DEFAULT NULL, ' age 'int( One)DEFAULT NULL,  PRIMARY KEY(' id ')) ENGINE=MyISAMDEFAULTCHARSET=UTF8MB4;

3. Writing Java code

 Packagetest;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classMysqldemo {Final StaticString jdbc_driver = "Com.mysql.cj.jdbc.Driver"; Final StaticString Db_url = "JDBC:MYSQL://LOCALHOST:3306/MV?SERVERTIMEZONE=UTC"; Final StaticString USER = "root"; Final StaticString PASS = "root";  Public Static voidMain (string[] args)throwsClassNotFoundException, SQLException {Connection conn=NULL; Statement stmt=NULL;        Class.forName (Jdbc_driver); System.out.println ("Connect Database ..."); Conn=drivermanager.getconnection (Db_url, USER, PASS); //Execute Querystmt =conn.createstatement (); String SQL= "SELECT * FROM Student"; ResultSet RS=stmt.executequery (SQL); //Expand result Set         while(Rs.next ()) {intid = rs.getint ("id"); String name= rs.getstring ("name"); intAge = Rs.getint ("Age"); System.out.println ("ID:" +ID); System.out.println ("Name:" +name); System.out.println ("Age:" +Age ); }    }}

Execution Result: Success

4. Problems encountered:

4.1 The server time zone value '? й??????? ' is unrecognized or represents more than one time zone

Solve:

Jdbc:mysql://LOCALHOST:3306/MV?SERVERTIMEZONE=UTC

4.2 Loading class ' Com.mysql.jdbc.Driver '. This is deprecated. The new driver class is ' com.mysql.cj.jdbc.Driver '. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary

Resolution: The original driver has been modified

Final Static String jdbc_driver = "Com.mysql.cj.jdbc.Driver";

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