• JDBC 增删改查


    增删改查

      1 package test03;
      2 
      3 import java.sql.Connection;
      4 import java.sql.DriverManager;
      5 import java.sql.ResultSet;
      6 import java.sql.Statement;
      7 /**
      8  * JDBC 增  
      9  *      删
     10  *      改
     11  *      查
     12  * @author star
     13  *
     14  */
     15 public class CURDTest {
     16     public static void main(String[] args) throws Exception {
     17         //insertTest();
     18         //deleteTest();
     19         //updateTest();
     20         selectTest();
     21     }
     22     //
     23     private static void selectTest() throws Exception {
     24         // TODO Auto-generated method stub
     25         //注册驱动
     26         Class.forName("com.mysql.jdbc.Driver");
     27         //创建连接
     28         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day01", "root", "root");
     29         //创建Statement对象
     30         Statement st = conn.createStatement();
     31         //sql查询
     32         String sql = "select * from stu where math<80";
     33         ResultSet rs = st.executeQuery(sql);//结果集
     34         //显示
     35         while(rs.next()) {
     36             int id = rs.getInt(1);
     37             String name =rs.getString(2);
     38             int age = rs.getInt(3);
     39             int math = rs.getInt(4);
     40             int english = rs.getInt(5);
     41             System.out.println(id+" "+name+" "+age+" "+math+" "+english);
     42             
     43         }
     44         rs.close();
     45         st.close();
     46         conn.close();
     47         
     48         
     49     }
     50 
     51     //
     52     private static void updateTest() throws Exception {
     53         // TODO Auto-generated method stub
     54         //注册驱动
     55         
     56         Class.forName("com.mysql.jdbc.Driver");
     57         //建立连接
     58         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day01", "root", "root");
     59         //创建statement对象
     60         Statement st = conn.createStatement();
     61         //执行sql语句
     62         String sql = "update stu set name = '小马哥' where id =1";
     63         int i = st.executeUpdate(sql);
     64         //判断是否修改
     65         if(i!=0) {
     66              System.out.println("修改成功");
     67         }
     68         //关闭资源
     69         st.close();
     70         conn.close();
     71         
     72     }
     73 
     74     //删除
     75     private static void deleteTest() throws Exception {
     76         // TODO Auto-generated method stub
     77         //注册驱动
     78         Class.forName("com.mysql.jdbc.Driver");
     79         //创建连接
     80         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/day01", "root", "root");
     81         //创建Statement对象
     82         Statement st = conn.createStatement();
     83         //  sql注入的问题
     84         //     在SQL语句中使用了系统自带的关键字 or and ,让where条件判断失效
     85         //String sql = "delete from stu where id= 1 or 1=1";//SQL注入问题,会把整张表的内容删除
     86         String sql = "delete from stu where id= 1";
     87         int i = st.executeUpdate(sql);
     88         if (i!=0) {
     89             System.out.println("删除成功");
     90         }
     91         st.close();
     92         conn.close();
     93     }
     94     //
     95     private static void insertTest() throws Exception {
     96         // TODO Auto-generated method stub
     97         //注册驱动
     98         Class.forName("com.mysql.jdbc.Driver");
     99         //创建连接
    100         Connection conn = DriverManager.getConnection("jdbc:mysql:///day01", "root", "root");
    101         //创建Statement对象
    102         Statement st = conn.createStatement();
    103         //执行语句
    104         String sql = "insert into stu values(null,'pp','50',70,50)";
    105         int i = st.executeUpdate(sql);
    106         //判断插入是否正确
    107         if (i!=0) {
    108             System.out.println("插入成功");
    109         }
    110         st.close();
    111         conn.close();
    112         
    113     }
    114 }
    增删改查

    jar包导入

    包: java.sql

      javax.sql

    1.加载驱动

    //DriverManager.registerDriver(new Driver());

    //通过源码发现Driver 类有一个静态代码块,自己创建一个对象添加进registerDriver(),我们自己又创建一个对象,造成两次注册
      //com.mysql.jdbc.Driver
      Class.forName("com.mysql.jdbc.Driver");

    //获取连接对象

    Connection coon = DriverManager.getConnection("jdbc:mysql://localhost:3306/day01","root","root");

    //获取statement对象

    Statement  st = coon.createStatement();

    //执行查询

    String sql = "select * from emp";

    ResultSet rs = st.executeQuery(sql);

    //获取结果集中的数据

    while(res.next()) {
       int id = res.getInt(1);
       String name = res.getString(2);
       String age = res.getString(3);
       int math = res.getInt(4);
       int english = res.getInt(5);
       System.out.println(id+" "+name +" "+age+" "+math+" "+english);
       
      }

    //释放资源

    rs.close();

    st.close();

    coon.close();

     1 package day28_test;
     2 
     3 
     4 import java.sql.Connection;
     5 import java.sql.DriverManager;
     6 import java.sql.ResultSet;
     7 import java.sql.SQLException;
     8 import java.sql.Statement;
     9 
    10 import com.mysql.jdbc.Driver;
    11 
    12 public class JDBC_Demo {
    13     public static void main(String[] args) throws Exception {
    14         //加载驱动
    15         DriverManager.registerDriver(new Driver());
    16         //获取连接对象
    17         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day01", "root", "root");
    18         //获取Statement
    19         Statement st = conn.createStatement();
    20         //执行查询
    21         String sql = "select * from stu";
    22         ResultSet res = st.executeQuery(sql);
    23         //获取结果集中的数据
    24         while(res.next()) {
    25             int id = res.getInt(1);
    26             String name = res.getString(2);
    27             String age = res.getString(3);
    28             int math = res.getInt(4);
    29             int english = res.getInt(5);
    30             System.out.println(id+" "+name +" "+age+" "+math+" "+english);
    31             
    32         }
    33         //释放资源
    34         res.close();
    35         st.close();
    36         conn.close();
    37     }
    38 }
    JDBC
  • 相关阅读:
    Phone-reset
    解决ie8下h5元素兼容性的问题
    PC css_reset
    centos7 nginx@1.16.1
    centos 7
    IE兼容css3的圆角和阴影和渐变
    前端开发安全编码规范
    防抖和节流封装模块
    vue的简单实现
    vue中$forceUpdate的使用
  • 原文地址:https://www.cnblogs.com/star521/p/9008302.html
Copyright © 2020-2023  润新知