java操作mysql的增删改查

本文提供了一个使用Java连接MySQL数据库的示例代码,演示了如何建立连接、执行增删改查操作,并展示查询结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其中,需要把mysql与java连接的jar(mysql-connector-java-5.1.6-bin.jar)包导入工程.

  1. package com.cn.edu;  
  2.   
  3. import java.beans.Statement;  
  4. import java.sql.Connection;  
  5. import java.sql.DriverManager;  
  6. import java.sql.PreparedStatement;  
  7. import java.sql.ResultSet;  
  8. import java.sql.SQLException;  
  9.   
  10. public class helloworld {  
  11.     private Connection conn = null;  
  12.     PreparedStatement statement = null;  
  13.   
  14.     // connect to MySQL  
  15.     void connSQL() {  
  16.         String url = "jdbc:mysql://localhost:3306/hello?characterEncoding=UTF-8";  
  17.         String username = "root";  
  18.         String password = "root"// 加载驱动程序以连接数据库   
  19.         try {   
  20.             Class.forName("com.mysql.jdbc.Driver" );   
  21.             conn = DriverManager.getConnection( url,username, password );   
  22.             }  
  23.         //捕获加载驱动程序异常  
  24.          catch ( ClassNotFoundException cnfex ) {  
  25.              System.err.println(  
  26.              "装载 JDBC/ODBC 驱动程序失败。" );  
  27.              cnfex.printStackTrace();   
  28.          }   
  29.          //捕获连接数据库异常  
  30.          catch ( SQLException sqlex ) {  
  31.              System.err.println( "无法连接数据库" );  
  32.              sqlex.printStackTrace();   
  33.          }  
  34.     }  
  35.   
  36.     // disconnect to MySQL  
  37.     void deconnSQL() {  
  38.         try {  
  39.             if (conn != null)  
  40.                 conn.close();  
  41.         } catch (Exception e) {  
  42.             System.out.println("关闭数据库问题 :");  
  43.             e.printStackTrace();  
  44.         }  
  45.     }  
  46.   
  47.     // execute selection language  
  48.     ResultSet selectSQL(String sql) {  
  49.         ResultSet rs = null;  
  50.         try {  
  51.             statement = conn.prepareStatement(sql);  
  52.             rs = statement.executeQuery(sql);  
  53.         } catch (SQLException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.         return rs;  
  57.     }  
  58.   
  59.     // execute insertion language  
  60.     boolean insertSQL(String sql) {  
  61.         try {  
  62.             statement = conn.prepareStatement(sql);  
  63.             statement.executeUpdate();  
  64.             return true;  
  65.         } catch (SQLException e) {  
  66.             System.out.println("插入数据库时出错:");  
  67.             e.printStackTrace();  
  68.         } catch (Exception e) {  
  69.             System.out.println("插入时出错:");  
  70.             e.printStackTrace();  
  71.         }  
  72.         return false;  
  73.     }  
  74.     //execute delete language  
  75.     boolean deleteSQL(String sql) {  
  76.         try {  
  77.             statement = conn.prepareStatement(sql);  
  78.             statement.executeUpdate();  
  79.             return true;  
  80.         } catch (SQLException e) {  
  81.             System.out.println("插入数据库时出错:");  
  82.             e.printStackTrace();  
  83.         } catch (Exception e) {  
  84.             System.out.println("插入时出错:");  
  85.             e.printStackTrace();  
  86.         }  
  87.         return false;  
  88.     }  
  89.     //execute update language  
  90.     boolean updateSQL(String sql) {  
  91.         try {  
  92.             statement = conn.prepareStatement(sql);  
  93.             statement.executeUpdate();  
  94.             return true;  
  95.         } catch (SQLException e) {  
  96.             System.out.println("插入数据库时出错:");  
  97.             e.printStackTrace();  
  98.         } catch (Exception e) {  
  99.             System.out.println("插入时出错:");  
  100.             e.printStackTrace();  
  101.         }  
  102.         return false;  
  103.     }  
  104.     // show data in ju_users  
  105.     void layoutStyle2(ResultSet rs) {  
  106.         System.out.println("-----------------");  
  107.         System.out.println("执行结果如下所示:");  
  108.         System.out.println("-----------------");  
  109.         System.out.println(" 用户ID" + "/t/t" + "淘宝ID" + "/t/t" + "用户名""/t/t" + "密码");  
  110.         System.out.println("-----------------");  
  111.         try {  
  112.             while (rs.next()) {  
  113.                 System.out.println(rs.getInt("ju_userID") + "/t/t"  
  114.                         + rs.getString("taobaoID") + "/t/t"  
  115.                         + rs.getString("ju_userName")  
  116.                          + "/t/t"+ rs.getString("ju_userPWD"));  
  117.             }  
  118.         } catch (SQLException e) {  
  119.             System.out.println("显示时数据库出错。");  
  120.             e.printStackTrace();  
  121.         } catch (Exception e) {  
  122.             System.out.println("显示出错。");  
  123.             e.printStackTrace();  
  124.         }  
  125.     }  
  126.   
  127.     public static void main(String args[]) {  
  128.   
  129.         helloworld h = new helloworld();  
  130.         h.connSQL();  
  131.         String s = "select * from ju_users";  
  132.   
  133.         String insert = "insert into ju_users(ju_userID,TaobaoID,ju_userName,ju_userPWD) values("+8329+","+34243+",'mm','789')";  
  134.         String update = "update ju_users set ju_userPWD =123 where ju_userName= 'mm'";  
  135.         String delete = "delete from ju_users where ju_userName= 'mm'";  
  136.   
  137.         if (h.insertSQL(insert) == true) {  
  138.             System.out.println("insert successfully");  
  139.             ResultSet resultSet = h.selectSQL(s);  
  140.             h.layoutStyle2(resultSet);  
  141.         }  
  142.         if (h.updateSQL(update) == true) {  
  143.             System.out.println("update successfully");  
  144.             ResultSet resultSet = h.selectSQL(s);     
  145.             h.layoutStyle2(resultSet);  
  146.         }  
  147.         if (h.insertSQL(delete) == true) {  
  148.             System.out.println("delete successfully");  
  149.             ResultSet resultSet = h.selectSQL(s);  
  150.             h.layoutStyle2(resultSet);  
  151.         }  
  152.           
  153.         h.deconnSQL();  
  154.     }  
  155. }  

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')"


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值