zoukankan      html  css  js  c++  java
  • 使用JDBC完成CRUD(增删改查)

    1.什么是JDBC
    JDBC是接口 是规范,本身sun公司没有实现 需要各大数据库厂商实现
    最原生的持久化技术 其他的框架都是都jdbc进行封装(增强拓展);
    要去连接数据库 就需要导入相应数据库对jdbc实现的jar包

    CRUD(增删改查)下面看java代码

    第一步:导包,我用的是MySQL,所有导MySQL的jdbc实现jar包,其他的数据库请找其他的数据库jar包

    public class JdbcDom{
        
        /**
         * 建表
         * @throws Exception 
         */
        @Test
        public void jianBiao() throws Exception {
            //贾:加载JDBC驱动(用反射的方式加载Driver(驱动)类)
            Class.forName("com.mysql.jdbc.Driver");
            //联:连接数据库(通过驱动管理器获取数据库连接("数据库地址","数据库账号","数据库秘密"))
            Connection connection = DriverManager.getConnection("jdbc:mysql:///2019_9_9","root","root");
            //欲:获得语句对象,
            //    上下语句是有关联的,只有获取了数据库连接对象才能拿到语句对象;
            //    获取语句对象,才能将sql语句执行到数据库
            Statement cs = connection.createStatement();
            
            
            //执:执行sql语句(cs.执行数据更新(CREATE TABLE 表名 (字段名1   数据类型(数据长度) primary key not null AUTO_INCREMENT,字段名2   数据类型(数据长度),....)))
            //primary key:设为主键
            //not null:不为空
            //AUTO_INCREMENT:自增
            cs.executeUpdate("CREATE TABLE tabeName2 (ID int primary key not null AUTO_INCREMENT,name varchar(10),age int)");
            
            
            //事:事务关闭
            cs.close();//关闭语句连接
            connection.close();//关闭数据库连接
        }
        
        
        
        
        /**
         * 删除指定id的数据
         * @throws Exception
         */
        @Test
        public void removeValue() throws Exception{
            //
            Class.forName("com.mysql.jdbc.Driver");
            //
            Connection connection = DriverManager.getConnection("jdbc:mysql:///2019_9_9","root","root");
            //
            Statement cs = connection.createStatement();
            //
            cs.executeUpdate("DELETE FROM tabename2 WHERE id=1");
            System.out.println("删除完成");
            //
            cs.close();
            connection.close();
        }
        
        
        /**
         * 添加数据
         * @throws Exception 
         */
        @Test
        public void addValue() throws Exception{
            //
            Class.forName("com.mysql.jdbc.Driver");
            //
            Connection connection = DriverManager.getConnection("jdbc:mysql:///2019_9_9","root","root");
            //
            Statement cs = connection.createStatement();
            //
            cs.executeUpdate("INSERT INTO tabename2 VALUES (null,'我去',20)");
            //
            cs.close();
            connection.close();
            
        }
        
        /**
         * 根据id修改数据
         * @throws Exception 
         */
        @Test
        public void setValue() throws Exception{
            //
            Class.forName("com.mysql.jdbc.Driver");
            //
            Connection connection = DriverManager.getConnection("jdbc:mysql:///2019_9_9","root","root");
            //
            Statement cs = connection.createStatement();
            //
            cs.executeUpdate("UPDATE tabename2 SET name='C',age=3 WHERE id=3");
            //
            cs.close();
            connection.close();
        }
        
        
        
        /**
         * 通过ID查询数据
         * @throws Exception 
         * 
         */
        @Test
        public void lookValue() throws Exception {
            //
            Class.forName("com.mysql.jdbc.Driver");
            //
            Connection connection = DriverManager.getConnection("jdbc:mysql:///2019_9_9","root","root");
            //
            Statement cs = connection.createStatement();
            //
            ResultSet e = cs.executeQuery("SELECT * FROM tabeName2 WHERE id=1");
            //要打印数据;判断e.next()不能少。
            if(e.next()){
                System.out.println(e.getInt("id")+e.getString("name")+e.getInt("age"));
            }else{
                System.out.println("没有");
            }
            //
            cs.close();
            connection.close();
            
        }

      /**
      *查询所有数据
      *
      */
    @Test
    public void lookAllValue() throws Exception{ // Class.forName("com.mysql.jdbc.Driver"); // Connection connection = DriverManager.getConnection("jdbc:mysql:///2019_9_9","root","root"); // Statement cs = connection.createStatement(); // ResultSet executeQuery = cs.executeQuery("SELECT * FROM tabename2"); while (executeQuery.next()) { System.out.println(executeQuery.getInt("id")+"__"+executeQuery.getString("name")+"__"+executeQuery.getInt("age")); } // cs.close(); connection.close(); } }

    虽然代码臃肿,但我能理解

  • 相关阅读:
    2019-2020-2 20175212童皓桢《网络对抗技术》 Exp4 恶意代码分析
    2019-2020-2 20175212童皓桢《网络对抗技术》Exp3 免杀原理与实践
    2019-2020-2 20175212童皓桢《网络对抗技术》Exp2 后门原理与实践
    2019-2020-2 20175212童皓桢《网络对抗技术》 Exp1 PC平台逆向破解
    2019-2020 《信息安全系统设计》20175212童皓桢 ucosii-2
    2019-2020-1 20175212_20175227《信息安全系统设计基础》
    实现mypwd
    2019-2020-1 20175212童皓桢《信息安全系统设计》实验四 外设驱动程序设计
    2019-2020-1 20175212童皓桢《信息安全系统设计》 实验三并发程序
    20175212-20175227 实验二 固件程序设计
  • 原文地址:https://www.cnblogs.com/bigbigxiao/p/11559126.html
Copyright © 2011-2022 走看看