使用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(); } }

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

原文地址:https://www.cnblogs.com/bigbigxiao/p/11559126.html