当前位置: 代码网 > it编程>编程语言>Java > Eclipse与MySQL数据库的连接(已实操)

Eclipse与MySQL数据库的连接(已实操)

2024年08月01日 Java 我要评论
成功实现eclipse和MySQL的连接,超详细教程

今天介绍的是eclipse如何与mysql相连,相信很多小伙伴和我一样,对路径啊,什么包放在那里啊很是头疼,今天一下午才弄好就赶来分享啦,超详细哦! 以下为我个人通过总结大家的方法,自己操作以后分享给大家,如有问题,评论提问,大家商讨解决。

准备工作:下载mysql和eclipse,这里就不讲了,注意的是你可能已经下载了mysql,但是没有下载jdbc,它们的下载是分开的,首先确保你的电脑已经装了eclipse和mysql,之后也许我会出安装教程(可私信)

1安装下载mysql的jar文件,并与mysql是同一版本很重要

打开mysql的官网,点击图中标红方框中的内容即可,如不理解(可私信)

 滑到最下面

选择connector/j(不要管workbench,这个是数据库的图形可视化页面) 

 这里注意一下:要选择与自己数据库版本相一致(eg:我的为8.0.29)

点击archives可以选择相应的版本

 

 根据自身习惯选择一个压缩包下载(个人选择zip)

 

如下图:看到红方框中的内容便是下载成功了 

 

接下来,讲一讲eclipse中的操作

 首先打开eclipse,创建一个新的java project,然后右击,如图:

 

 

出现如下图所示证明已连接好: 

 

最后,创建数据库来验证以下(该部分参考了菜鸟教程java部分mysql的讲解和代码)

(1)打开自己的mysql

 

 

 (2)在数据库中创建表

create table `websites` (
  `id` int(11) not null auto_increment,
  `name` char(20) not null default '' comment '站点名称',
  `url` varchar(255) not null default '',
  `alexa` int(11) not null default '0' comment 'alexa 排名',
  `country` char(10) not null default '' comment '国家',
  primary key (`id`)
) engine=innodb auto_increment=10 default charset=utf8;

 

(3)向表中插入数据(忘记截图了)

insert into `websites` values
 ('1', 'google', 'https://www.google.cm/', '1', 'usa'),
 ('2', '淘宝', 'https://www.taobao.com/', '13', 'cn'),
 ('3', '菜鸟教程', 'http://www.runoob.com', '5892', ''),
 ('4', '微博', 'http://weibo.com/', '20', 'cn'),
 ('5', 'facebook', 'https://www.facebook.com/', '3', 'usa');

 

(4)查看所创表结构和表的数据 

 

 (5)打开eclipse,在其中输入如下代码:


import java.sql.*;
 
public class mysqldemo {
 
    // mysql 8.0 以下版本 - jdbc 驱动名及数据库 url
    //static final string jdbc_driver = "com.mysql.jdbc.driver";  
    //static final string db_url = "jdbc:mysql://localhost:3306/runoob";
 
    // mysql 8.0 以上版本 - jdbc 驱动名及数据库 url
    static final string jdbc_driver = "com.mysql.cj.jdbc.driver";  
    static final string db_url = "jdbc:mysql://localhost:3306/runoob?usessl=false&allowpublickeyretrieval=true&servertimezone=utc";//这里为自己表所在的数据库名称
 
 
    // 数据库的用户名与密码,需要根据自己的设置
    static final string user = "root";
    static final string pass = "000000";
 
    public static void main(string[] args) {
        connection conn = null;
        statement stmt = null;
        try{
            // 注册 jdbc 驱动
            class.forname(jdbc_driver);
        
            // 打开链接
            system.out.println("连接数据库...");
            conn = drivermanager.getconnection(db_url,user,pass);
        
            // 执行查询
            system.out.println(" 实例化statement对象...");
            stmt = conn.createstatement();
            string sql;
            sql = "select id, name, url from websites";
            resultset rs = stmt.executequery(sql);
        
            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getint("id");
                string name = rs.getstring("name");
                string url = rs.getstring("url");
    
                // 输出数据
                system.out.print("id: " + id);
                system.out.print(", 站点名称: " + name);
                system.out.print(", 站点 url: " + url);
                system.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(sqlexception se){
            // 处理 jdbc 错误
            se.printstacktrace();
        }catch(exception e){
            // 处理 class.forname 错误
            e.printstacktrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(sqlexception se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(sqlexception se){
                se.printstacktrace();
            }
        }
        system.out.println("goodbye!");
    }
}

验证:成功实现eclipse与数据库的连接 

 

 

 

 

 

 

 

 

 

 

 

 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com