zoukankan      html  css  js  c++  java
  • PreparedStatement的增删改

    案例9:preparedStatement增删改:

    package com.java.JDBC;
    
    
    import java.sql.*;
    
    
    public class JDBCTest09 {
        public static void main(String[] args) {
            
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            
            try {
                // 1 注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 2 获取连接
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","root","333");
                // 3 获取预编译数据库操作对象
                
                // 插入数据
                /*String sql = "insert into dept(deptno,dname,loc) values(?,?,?)";
                ps = conn.prepareStatement(sql);
        
                ps.setString(1,"60");
                ps.setString(2,"销售部");
                ps.setString(3,"上海");*/
                
                // 更新数据
                /*String sql = "update dept set dname = ?, loc = ? where deptno = ?";
                ps = conn.prepareStatement(sql);
        
                ps.setString(1,"研发一部");
                ps.setString(2,"重庆");
                ps.setString(3,"60");*/
                
                // 删除数据
                String sql = "delete from dept where deptno = ?";
                ps = conn.prepareStatement(sql);
                
                ps.setString(1,"60");
                
                // 4 执行sql语句
                int count = ps.executeUpdate();
                // System.out.println(count == 1 ? "插入成功" : "插入失败");
                System.out.println(count == 1 ? "删除成功" : "删除失败");
                
                // 5 处理返回结果集
                
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 6 释放资源
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
        
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
        
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  • 相关阅读:
    2019年春季第四周作业
    第三周作业
    第二周作业
    最大值及其下标
    查找整数
    PTA3
    币值转换
    三位老师
    自我介绍
    poj 3278 Catch That Cow(bfs)
  • 原文地址:https://www.cnblogs.com/xlwu/p/13654074.html
Copyright © 2011-2022 走看看