zoukankan      html  css  js  c++  java
  • IDEA中使用JDBC访问SQL SERVER(五)修改结果集数据

    package com.example.wfydemo.controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.sql.*;
    
    @RestController
    public class UpdateResultSet {
        @RequestMapping("/UpdateResultSet")
        public String UpdateResultSet() {
    
            // Create a variable for the connection string.
            String connectionUrl = "jdbc:sqlserver://127.0.0.1:1433;databaseName=springbootTestDB;user=sa;password=6617saSA";//sa身份连接
    
            try (Connection con = DriverManager.getConnection(connectionUrl);
                 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);) {
    
                // Create and execute an SQL statement, retrieving an updateable result set.
                String SQL = "SELECT * FROM T_User ;";
                ResultSet rs = stmt.executeQuery(SQL);
                displayRow("原来的内容:", rs);
    
                // Insert a row of data.
                rs.moveToInsertRow();
                rs.updateString("name", "user10");
                rs.updateString("password", "121212");
                rs.insertRow();
    
                // Retrieve the inserted row of data and display it.
                SQL = "SELECT * from T_User WHERE name = 'user10';";
                rs = stmt.executeQuery(SQL);
                displayRow("ADDED ROW", rs);
    
                // Update the row of data.
                rs.first();
                rs.updateString("password", "0123456789");
                rs.updateRow();
    
                // Retrieve the updated row of data and display it.
                rs = stmt.executeQuery(SQL);
                displayRow("UPDATED ROW", rs);
    
                // Delete the row of data.
                rs.first();
                rs.deleteRow();
                System.out.println("ROW DELETED");
    
                //查询结果为空,插入一条记录
                SQL = "SELECT * FROM T_User where 1 = 2;";
                rs = stmt.executeQuery(SQL);
                rs.moveToInsertRow();
                rs.updateString("name", "user插入");
                rs.updateString("password", "123321");
                rs.insertRow();
                SQL = "SELECT * from T_User  ;";
                rs = stmt.executeQuery(SQL);
                displayRow("insert", rs);
            }
            // Handle any errors that may have occurred.
            catch (SQLException e) {
                e.printStackTrace();
            }
            return "UpdateResultSet";
        }
    
        private static void displayRow(String title,
                                       ResultSet rs) throws SQLException {
            System.out.println(title);
            while (rs.next()) {
                System.out.println(rs.getString("name") + " : " + rs.getString("password"));
            }
        }
    }
    

      

  • 相关阅读:
    css3 让一个图片翻转示例代码
    万能清除法
    str_replace 字符串匹配替换 explode 拆分字符串成数组 implode 数组 为字符串 list($month, $day, $year) = split ('[/.-]', $date);
    历年沪深A股、香港H股票数据导入和实时数据更新展示 ---转载
    CSS3生成音频波纹效果加载中动画
    jQuery中Ajax快捷方法之$.getScript()
    jQuery中Ajax快捷方法之$.get()
    ajax 关于IP地址查询的API
    php 拒绝用户输入非法字符
    PHP 字符串函数是 PHP 核心的组成部分。
  • 原文地址:https://www.cnblogs.com/wfy680/p/14965942.html
Copyright © 2011-2022 走看看