连接mysql: 新的MySQL驱动模板和mysql-connector-java-8-0-12

版权声明:署名-非商业性使用 转载请保留原文链接及作者。 https://blog.csdn.net/qq_40413396/article/details/86492015


新的MySQL驱动8-0-12

之前链接本地数据库用了一个5.X版本的:如图

logo

我的数据库版本 如图:

在这里插入图片描述

run之后;一直报了一个链接不了数据库的错误,一番操作之后启用了8.X的版本;运行之后如图 :

在这里插入图片描述

运行成功

mysql-connector-java.8.0.12本版下载

[https://pan.baidu.com/s/16ZkrIpEIrALBNfokx2BXsw:https://pan.baidu.com/s/16ZkrIpEIrALBNfokx2BXsw “百度盘”)

百度盘密码 vt9o

主程序

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication4;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.sql.PreparedStatement;

/**
 *
 * @author zhang
 */
public class JavaApplication4 {
    static String selectsql = null;
    static ResultSet retsult = null;
    public static final String url = "jdbc:mysql://localhost:3306/intnation";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String password = "xxxx";

    public static Connection conn = null;
    public static PreparedStatement pst = null;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int paraCount = 5; //读取参数数量
        selectsql = "select * from tb_emp";//SQL语句

        try {
            Class.forName(name);//指定连接类型
            conn = DriverManager.getConnection(url, user, password);//获取连接
            pst = conn.prepareStatement(selectsql);//准备执行语句
        } catch (Exception e) {
            e.printStackTrace();
        }

        String [] paras = new String [paraCount];
        try {
            retsult = pst.executeQuery();//执行语句,得到结果集

            while (retsult.next()) {
            for(int i = 0;i<paraCount;i++){
            paras[i] = retsult.getString(i+2);
            }
            System.out.println(Arrays.toString(paras));

            }//显示数据
            retsult.close();
            conn.close();//关闭连接
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_40413396/article/details/86492015