温馨提示×

温馨提示×

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

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

使用java连接mysql数据库并实现增删改查等操作

发布时间:2020-11-18 15:53:15 来源:亿速云 阅读:947 作者:Leah 栏目:编程语言

使用java连接mysql数据库并实现增删改查等操作?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

首先,需要把MySQL与Java连接的jar(mysql-connector-java-5.1.6-bin.jar)包导入工程.

package com.cn.edu;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class helloworld {
  private Connection conn = null;
  PreparedStatement statement = null;
  // connect to MySQL
  void connSQL() {
    String url = "jdbc:mysql://localhost:3306/hello?characterEncoding=UTF-8";
    String username = "root";
    String password = "root"; // 加载驱动程序以连接数据库
    try {
      Class.forName("com.mysql.jdbc.Driver" );
      conn = DriverManager.getConnection( url,username, password );
      }
    //捕获加载驱动程序异常
     catch ( ClassNotFoundException cnfex ) {
       System.err.println(
       "装载 JDBC/ODBC 驱动程序失败。" );
       cnfex.printStackTrace();
     }
     //捕获连接数据库异常
     catch ( SQLException sqlex ) {
       System.err.println( "无法连接数据库" );
       sqlex.printStackTrace();
     }
  }
  // disconnect to MySQL
  void deconnSQL() {
    try {
      if (conn != null)
        conn.close();
    } catch (Exception e) {
      System.out.println("关闭数据库问题 :");
      e.printStackTrace();
    }
  }
  // execute selection language
  ResultSet selectSQL(String sql) {
    ResultSet rs = null;
    try {
      statement = conn.prepareStatement(sql);
      rs = statement.executeQuery(sql);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return rs;
  }
  // execute insertion language
  boolean insertSQL(String sql) {
    try {
      statement = conn.prepareStatement(sql);
      statement.executeUpdate();
      return true;
    } catch (SQLException e) {
      System.out.println("插入数据库时出错:");
      e.printStackTrace();
    } catch (Exception e) {
      System.out.println("插入时出错:");
      e.printStackTrace();
    }
    return false;
  }
  //execute delete language
  boolean deleteSQL(String sql) {
    try {
      statement = conn.prepareStatement(sql);
      statement.executeUpdate();
      return true;
    } catch (SQLException e) {
      System.out.println("插入数据库时出错:");
      e.printStackTrace();
    } catch (Exception e) {
      System.out.println("插入时出错:");
      e.printStackTrace();
    }
    return false;
  }
  //execute update language
  boolean updateSQL(String sql) {
    try {
      statement = conn.prepareStatement(sql);
      statement.executeUpdate();
      return true;
    } catch (SQLException e) {
      System.out.println("插入数据库时出错:");
      e.printStackTrace();
    } catch (Exception e) {
      System.out.println("插入时出错:");
      e.printStackTrace();
    }
    return false;
  }
  // show data in ju_users
  void layoutStyle2(ResultSet rs) {
    System.out.println("-----------------");
    System.out.println("执行结果如下所示:");
    System.out.println("-----------------");
    System.out.println(" 用户ID" + "/t/t" + "淘宝ID" + "/t/t" + "用户名"+ "/t/t" + "密码");
    System.out.println("-----------------");
    try {
      while (rs.next()) {
        System.out.println(rs.getInt("ju_userID") + "/t/t"
            + rs.getString("taobaoID") + "/t/t"
            + rs.getString("ju_userName")
             + "/t/t"+ rs.getString("ju_userPWD"));
      }
    } catch (SQLException e) {
      System.out.println("显示时数据库出错。");
      e.printStackTrace();
    } catch (Exception e) {
      System.out.println("显示出错。");
      e.printStackTrace();
    }
  }
  public static void main(String args[]) {
    helloworld h = new helloworld();
    h.connSQL();
    String s = "select * from ju_users";
    String insert = "insert into ju_users(ju_userID,TaobaoID,ju_userName,ju_userPWD) values("+8329+","+34243+",'mm','789')";
    String update = "update ju_users set ju_userPWD =123 where ju_userName= 'mm'";
    String delete = "delete from ju_users where ju_userName= 'mm'";
    if (h.insertSQL(insert) == true) {
      System.out.println("insert successfully");
      ResultSet resultSet = h.selectSQL(s);
      h.layoutStyle2(resultSet);
    }
    if (h.updateSQL(update) == true) {
      System.out.println("update successfully");
      ResultSet resultSet = h.selectSQL(s);
      h.layoutStyle2(resultSet);
    }
    if (h.insertSQL(delete) == true) {
      System.out.println("delete successfully");
      ResultSet resultSet = h.selectSQL(s);
      h.layoutStyle2(resultSet);
    }
    h.deconnSQL();
  }
}

notice:

1、现在一般用的驱动是com.mysql.jdbc.Driver,以前的那个什么org的驱动虽然封装了com.mysql.jdbc.Driver,但不好用,过时了。

2、prepareStatement(sql)statement的子类,比statement好用。

3、如果数据库中定义的是int值,那么sql语句中要把int单独提出来。如".....values("+8329+","+34243+",'mm','789')"

关于使用java连接mysql数据库并实现增删改查等操作问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

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

AI