JDBC连接MySQL5.7.14

1、官网下载jdbc connector 5.1.37

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

JDBC连接MySQL5.7.14_第1张图片


2、从下载的的zip文件中取出mysql-connector-java-5.1.31-bin.jar,放到工程lib中并引用

JDBC连接MySQL5.7.14_第2张图片

JDBC连接MySQL5.7.14_第3张图片


3、参考文档例子创建连接并进行查询操作

创建连接:http://dev.mysql.com/doc/connectors/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection-drivermanager

进行查询操作:http://dev.mysql.com/doc/connectors/en/connector-j-usagenotes-statements.html#connector-j-examples-execute-select

/**
 * 文件名:ConnectMysql.java
 * 版本信息:Version 1.0
 * 日期:2016年9月2日
 * Copyright talkweb.com.cn Corporation 2016
 * 版权所有
 */
package com.zjr.test.jdbc.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 类描述:
 * 
 * @version: 1.0
 * @author: Administrator
 * @version: 2016年9月2日 下午9:45:53
 */
public class ConnectMysql
{

    /**
     * 方法描述:
     * 
     * @param:
     * @return:
     * @version: 1.0
     * @author: Administrator
     * @version: 2016年9月2日 下午9:45:53
     */
    public static void main(String[] args)
    {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/zjrtest?"
                    + "user=abc&password=cba"
                    + "&useUnicode=true&characterEncoding=UTF8");
            System.out.println(conn);
            // Do something with the Connection
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT name FROM MyClass");
            // or alternatively, if you don't know ahead of time that
            // the query will be a SELECT...
            if (stmt.execute("SELECT name FROM MyClass"))
            {
                rs = stmt.getResultSet();
                if(rs.next())
                {
                    String name = rs.getString(1);
                    System.out.println(name);
                }
            }
            // Now do something with the ResultSet ....
        }
        catch (SQLException ex)
        {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            // it is a good idea to release
            // resources in a finally{} block
            // in reverse-order of their creation
            // if they are no-longer needed
            if (rs != null)
            {
                try
                {
                    rs.close();
                }
                catch (SQLException sqlEx)
                {
                } // ignore
                rs = null;
            }
            if (stmt != null)
            {
                try
                {
                    stmt.close();
                }
                catch (SQLException sqlEx)
                {
                } // ignore
                stmt = null;
            }
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException sqlEx)
                {
                } // ignore
                conn = null;
            }
        }
    }

}


你可能感兴趣的:(MySQL)