zoukankan      html  css  js  c++  java
  • Eclipse连接MySQL数据库(傻瓜篇)

          本来不想写这么简单人文章,在百度上搜索我这个标题,完全符合标题的一大堆。但我按照那些文章捣鼓了很久,就是不行。

    我的环境:MySQL:mysql-essential-5.1.51-win32

         jdbc驱动:我已经上传到csdn上一个:http://download.csdn.net/source/3451945

         Eclipse:任意版本,免费的,可以百度的到。

    1。MySQL安装,不会的朋友可以看连接:http://www.duote.com/tech/1/2430_1.html

        下面来创建一个数据: 

    mysql>CREATE   DATABASE test;   //创建一个数据库

    mysql
    >use test; //指定test为当前要操作的数据库

    mysql
    >CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //创建一个表user,设置两个字段。

    mysql
    >INSERT INTO user VALUES('huzhiheng','123456'); //插入一条数据到表中

      

    2。打开Eclipse,创建一个项目(my),

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

    我的项目列表:

     

     

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

    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  

     

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

     
     
    4。可以查看到MySQL里面的内容,那我们是不是想往MySQL中插入数据呢。
     
    下面的例子,往MySQL的user表中插入100条数据
    import java.sql.*;

    publicclass Myjproject {
    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");

    int num=100;
    PreparedStatement Statement
    =connect.prepareStatement("INSERT INTO user VALUES(?,?)");
    for(int i=0;i<num;i++) //定义个100次的循环,往表里插入一百条信息。
    {
    Statement.setString(
    1,"chongshi"+i);
    Statement.setString(
    2,"bo"+i);
    Statement.executeUpdate();
    }

    // } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    // System.out.println("An error has occurred:"+e.toString());
    // e.printStackTrace();
    }catch(SQLException e)
    {
    }
    }
    }

     

    5.下面我们打开MySQL数据库进行查看 

     
    mysql> show databases;  //查看所数据库
    mysql> use  test;    //使test为当前要操作的数据库
    mysql> show tables; //查看当前数据库的所有表
    mysql> select *from user;  //查看当前表(user)的所有信息  
     

     

    注意:如果不能正常连接你的数据库,请检查你代码中,驱动、用户名、密码、表等信息是否对应无误,不要把别人的代码直接复制过来,看也不看就用。

  • 相关阅读:
    IDEA插件之 CodeGlance(无需滚动的代码地图)
    【翻译】面向自然语言处理的深度学习(一)
    如何估算神经网络参数占用多少内存或显存容量
    Latex向上向下取整语法 及卷积特征图高宽计算公式编辑
    自动测试LeetCode用例方法
    C# Wpf 文件保存对话框
    YOLO实践初探
    前中后序递归遍历树的体会 with Python
    Python 中日期函数
    Tensorflow Windows安装
  • 原文地址:https://www.cnblogs.com/fnng/p/2110023.html
Copyright © 2011-2022 走看看