How Java connects to MySQL database

Source: Internet
Author: User

JDBC (Java Data Base Connectivity,java database connection) is a Java API for executing SQL statements that provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. JDBC provides a benchmark to build more advanced tools and interfaces that enable database developers to write database applications.

If you want to use the database to add the driver of the database, different databases have not the driver, here is not explained, the method of adding Jar program driver package is not explained here,

It is necessary to connect to the database when using JDBC programming, import the jar package is necessary, import the other Jar package method is same, the import method is

Open Eclipse

1. Right-click the project to import the jar package, click Properties
2. Choose Java build path on the left and select libraries on the right.
3. Select Add External jars
4. Select the jar package according to the path
OK, it's okay.

Java connection to MySQL's latest driver package

Http://www.mysql.com/downloads/connector/j

There are two ways to import a jar package, the first is to put the jar package in the project directory, by adding a jar package, the use of relative address, so that the project copy to another computer can also be used

The second method is to import the external jar package, which is the absolute address if the item is to be copied to another computer and re-imported

The first method, it is recommended to use

The second method of

Once added, the following will appear

---------------------------------------------------------------------------------------------------------

Demo is as follows

importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Connection;importjava.sql.Statement;publicclass MysqlDemo {    publicstaticvoidmain(String[] args) throwsException {        Connection conn = null;        String sql;        // MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值        // 避免中文乱码要指定useUnicode和characterEncoding        // 执行数据库操作之前要在数据库管理系统上创建一个数据库,名字自己定,        // 下面语句之前就要先创建javademo数据库        String url = "jdbc:mysql://localhost:3306/javademo?"                + "user=root&password=root&useUnicode=true&characterEncoding=UTF8/useSSL=false";        try{            // 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,            // 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以            Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动            // or:            // com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();            // or:            // new com.mysql.jdbc.Driver();            System.out.println("成功加载MySQL驱动程序");            // 一个Connection代表一个数据库连接            conn = DriverManager.getConnection(url);            // Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等            Statement stmt = conn.createStatement();            sql = "create table student(NO char(20),name varchar(20),primary key(NO))";            int result = stmt.executeUpdate(sql);// executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功            if(result != -1) {                System.out.println("创建数据表成功");                sql = "insert into student(NO,name) values(‘2012001‘,‘陶伟基‘)";                result = stmt.executeUpdate(sql);                sql = "insert into student(NO,name) values(‘2012002‘,‘周小俊‘)";                result = stmt.executeUpdate(sql);                sql = "select * from student";                ResultSet rs = stmt.executeQuery(sql);// executeQuery会返回结果的集合,否则返回空值                System.out.println("学号\t姓名");                while(rs.next()) {                    System.out                            .println(rs.getString(1) + "\t"+ rs.getString(2));// 入如果返回的是int类型可以用getInt()                }            }        } catch(SQLException e) {            System.out.println("MySQL操作错误");            e.printStackTrace();        } catch(Exception e) {            e.printStackTrace();        } finally {            conn.close();        }    }}----------------------------------------------------------------------------------------------------------problems encountered and solutions: 1. PresenceErrorJava.lang.ClassNotFoundException:com.mysql.jdbc.Driver

Reason: Importing a package simply imports the jar package without having to import the entire zip

2. Appears

Wed Dec 22:46:52 CET warn:establishing SSL connection without server ' s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must is established by default if explicit Opti On isn ' t set. For compliance with existing applications not using SSL, the Verifyservercertificate property was set to ' false '. You need either to explicitly disable SSL by setting Usessl=false, or set usessl=true and provide truststore for server CE Rtificate verification.

The reason is

 String url = "jdbc:mysql://localhost:3306/javademo?"                + "user=root&password=root&useUnicode=true&characterEncoding=UTF8/useSSL=false";Here's the last thing to remember to write Usessl=false that means to turn off Secure Sockets Layer and ignore SSL errors

How Java connects to 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.