JDBC連接MySQL5.7.14

1、官網下載jdbc connector 5.1.37

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


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




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;
            }
        }
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章