• 2016-7-8 JDBC for Mysql


    版本:

    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" 必须滴

    3.在JDK1.5之后,其实已经不需要去显式调用Class.forName("com.mysql.cj.jdbc.Driver")了,DriverManager会自动去加载合适的驱动,但是前提是CLASSPATH下必须有驱动jar包

  • 相关阅读:
    elementui form-item中多个字段校验
    Element-ui的 el-form 使用 v-if校验失灵问题
    fullcalendar title换行
    html拼接
    element-ui cascader 省市区 动态加载
    切换vue项目初始化路径
    用JavaScript获取当月第一天和最后一天
    小宝和小黑
    python目录
    3
  • 原文地址:https://www.cnblogs.com/juzi-123/p/5654353.html
Copyright © 2020-2023  润新知