码迷,mamicode.com
首页 > 数据库 > 详细

2016-7-8 JDBC for Mysql

时间:2016-07-08 19:50:12      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

版本:

mysql-connector-java-6.0.2-bin.jar

mysql : 5.7.11

import com.mysql.cj.api.jdbc.Statement;

public class test {

    public static void main(String[] args) {
        try {

            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
            Connection conn = null;
            String urlString = "jdbc:mysql://localhost:3306/javademo?user=root&password=123" 
                    + "&characterEncoding=utf8"
                        + "&useSSL=true&serverTimezone=UTC";
            conn = DriverManager.getConnection(urlString);
            Statement sat = (Statement) conn.createStatement();
            String sql = "select * from student";
            ResultSet result = sat.executeQuery(sql);
            int numCol = result.getMetaData().getColumnCount();
            while (result.next()) {
                for (int i = 1; i <= numCol; i++) {
                    System.out.println(result.getString(i));
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

技术分享

更新:

1.Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

2."&useSSL=true&serverTimezone=UTC" 必须滴

2016-7-8 JDBC for Mysql

标签:

原文地址:http://www.cnblogs.com/juzi-123/p/5654353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!