温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql与eclipse建立连接的方法

发布时间:2020-06-28 18:09:17 来源:亿速云 阅读:350 作者:清晨 栏目:编程语言

这篇文章主要介绍mysql与eclipse建立连接的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

mysql与eclipse建立连接的步骤:

1、打开Eclipse,创建一个项目(my),

操作:右键点击my--->build Path--->add external Archiver...选择jdbc驱动,点击确定。

mysql与eclipse建立连接的方法

我的项目列表:

mysql与eclipse建立连接的方法

2、驱动已经导入,下面我们来写一个程序验证一下

import java.sql.*;
publicclass MysqlJdbc {
  publicstaticvoid main(String args[]) {
    try {
      Class.forName("com.mysql.jdbc.Driver");     //加载MYSQL JDBC驱动程序   
      //Class.forName("org.gjt.mm.mysql.Driver");
     System.out.println("Success loading Mysql Driver!");
    }
    catch (Exception e) {
      System.out.print("Error loading Mysql Driver!");
      e.printStackTrace();
    }
    try {
      Connection connect = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/test","root","198876");
           //连接URL为   jdbc:mysql//服务器地址/数据库名  ,后面的2个参数分别是登陆用户名和密码
      System.out.println("Success connect Mysql server!");
      Statement stmt = connect.createStatement();
      ResultSet rs = stmt.executeQuery("select * from user");
                                                              //user 为你表的名称
while (rs.next()) {
        System.out.println(rs.getString("name"));
      }
    }
    catch (Exception e) {
      System.out.print("get data error!");
      e.printStackTrace();
    }
  }
}

点击运行程序:  

Success loading Mysql Driver!

Success connect Mysql server!

huzhiheng  

出现上面结果,说明连接数据库成功。

以上是mysql与eclipse建立连接的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI