zoukankan      html  css  js  c++  java
  • JDBC编程之数据更新

    -------------------siwuxie095

       

       

       

       

       

       

       

    JDBC 编程之数据更新

       

       

       

    首先下载 MySQL 的 JDBC 驱动,下载链接:

    https://dev.mysql.com/downloads/connector/j/

       

       

       

       

    mysql-connector-java-5.1.41.zip 解压后一览:

       

       

       

       

       

    工程名:JDBCTest

    包名:com.siwuxie095.jdbc

    类名:JDBCTestX.java

       

       

    打开资源管理器,在工程 JDBCTest 文件夹下,创建一个文件夹:lib,

    在其中放入:mysql-connector-java-5.1.41-bin.jar

       

       

    工程结构目录如下:

       

       

       

       

    选择 mysql-connector-java-5.1.41-bin.jar,右键->Build Path->Add to Build Path

       

    此时,工程结构目录一览:

       

       

       

       

       

    代码:

       

    package com.siwuxie095.jdbc;

       

    import java.sql.DriverManager;

    import java.sql.Connection;

    import java.sql.Statement;

       

       

    /**

    * 下面的方法实际上将数据库信息硬编码到java代码中,不可取

    *

    * @author siwux

    *

    */

    public class JDBCTestX {

     

    /**

    * jdbc编程流程:

    * 加载驱动

    * 打开连接

    * 执行查询

    * 处理结果

    * 清理环境

    */

     

    //getConnection() 获取数据库连接

    public static Connection getConnection() {

     

    Connection conn=null;

     

    try {

     

    Class.forName("com.mysql.jdbc.Driver");

    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sims_db","root","8888");

     

    } catch (Exception e) {

    e.printStackTrace();

    System.err.println("加载数据库失败...");

    }

    return conn;

    }

     

     

    public static void insert() {

     

    Connection conn=getConnection();

     

    try {

     

    String sql="insert into stu_password(stu_id,stu_pwd)"+

    "values('005','005')";

     

    Statement st=conn.createStatement();

     

    //executeUpdate() 可以执行 DML 语句中的 insert delete update

    //返回影响的记录条数

    //如果执行的是 DDL 语句,则返回 0

    int count=st.executeUpdate(sql);

    System.out.println("stu_password表中插入了 "+count+" 条记录");

    conn.close();

     

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

     

     

    public static void update() {

     

    Connection conn=getConnection();

     

    try {

     

    String sql="update stu_password set Stu_pwd='000' where stu_id='001'";

     

    Statement st=conn.createStatement();

     

    int count=st.executeUpdate(sql);

    System.out.println("stu_password表中更新了 "+count+" 条记录");

    conn.close();

     

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

     

     

    public static void delete() {

     

    Connection conn=getConnection();

     

    try {

     

    String sql="delete from Stu_password where stu_id='001'";

     

    Statement st=conn.createStatement();

     

    int count=st.executeUpdate(sql);

    System.out.println("stu_password表中删除了 "+count+" 条记录");

    conn.close();

     

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

     

     

    public static void deleteX() {

     

    Connection conn=getConnection();

     

    try {

     

    String sql="delete from stu_info where stu_id='001'";

     

    Statement st=conn.createStatement();

     

    int count=st.executeUpdate(sql);

    System.out.println("stu_info表中删除了 "+count+" 条记录");

    conn.close();

     

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

     

     

    public static void main(String[] args) {

     

    //insert();

    //update();

    //delete();

    deleteX();

    }

     

    }

       

       

       

    注意:高版本的 JDBC 驱动需要指明是否进行 SSL 连接

       

       

    加上:?characterEncoding=utf8&useSSL=false

       

    或:

       

       

    加上:?useUnicode=true&characterEncoding=utf-8&useSSL=false

       

       

       

    总结 JDBC 编程流程:

       

    1)加载驱动:加载 JDBC 驱动程序

       

    2)打开连接:打开一个数据库连接

       

    3)执行查询:创建一个会话对象,执行增删改查等操作

       

    4)处理结果:处理查询的结果

       

    5)清理环境:关闭会话,关闭连接等操作,完成资源的清理工作

       

       

       

       

    关于 数据库的准备,详见本人博客的分类:来一杯Java

    里面的 JDBC编程之数据准备

       

    本人博客(任选其一)链接:

    https://www.baidu.com/s?ie=UTF-8&wd=siwuxie095

       

       

       

       

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    路飞学城Python-Day23
    JS中异常处理的理解
    JS获取浏览器中的各种宽高值
    浏览器兼容性处理大全
    js中点击事件方法三种方式的区别
    js 中继承的几种方式
    理解JS的6种继承方式
    理解javascript中的事件模型
    Javascript 原型链之原型对象、实例和构造函数三者之间的关系
    对于js原型和原型链继承的简单理解
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/6695452.html
Copyright © 2011-2022 走看看