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

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一码归一码归一码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值