美文网首页
jdbc-增删改查

jdbc-增删改查

作者: 金厚琦 | 来源:发表于2018-10-23 19:24 被阅读0次

    package com.neuedu.jdbc;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.sql.Statement;

    import org.junit.Test;

    public class Insert {

    // 查询 select select * from 表名 where 字段名=''

    @Test

    public void select() throws ClassNotFoundException, SQLException {

    /*

    * 创建jdbc应用步骤分7步 1)载入jdbc驱动(指定我要连接到哪种数据库,连接不同数据库用不同驱动)

    *

    * 2)定义连接url(连接准备 url,端口,用户名,密码) 3)建立连接 4)创建PreparedStatement(动态)

    * Statement(静态的)(拼sql语句) 5)执行数据库命令(crud) 6)结果的处理 7)关闭连接

    */

    // com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();

    // 1)载入jdbc驱动(指定我要连接到哪种数据库,连接不同数据库用不同驱动)

    Class.forName("com.mysql.jdbc.Driver");// (Drive:驱动)

    // 2)定义连接url(连接准备 url,端口,用户名,密码)

    String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";//数据库地址

    String user = "root";//数据库用户名

    String password = "root";//数据库密码

    // 3)建立连接

    Connection conn = DriverManager.getConnection(url, user, password);

    // 4)创建PreparedStatement(动态) Statement(静态的)(拼sql语句)

    Statement st = conn.createStatement();

    // 5)执行数据库命令(crud)

    // executeUpdate insert update delete

    // executeQuery select 用于查询

    ResultSet rs = st.executeQuery("select * from emp where deptno=10");// 用ResultSet接,返回结果集

    // 6)结果的处理

    while (rs.next()) {// 不知道有几条 用while循环

    // 双引号里是数据库的字段名

    int emp_no = rs.getInt("empno");

    System.out.println(emp_no);

    String e_name = rs.getString("ename");

    System.out.println(e_name);

    int de_ptno = rs.getInt("deptno");

    System.out.println(de_ptno);

    }

    // 7)关闭连接

    rs.close();

    st.close();

    conn.close();

    }

    // 发现异常不显示,改造代码

    @Test

    public void select1() {

    Connection conn = null;

    Statement st = null;

    ResultSet rs = null;

    try {

    // 1)载入jdbc驱动(指定我要连接到哪种数据库,连接不同数据库用不同驱动)

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

    // 2)定义连接url(连接准备 url,端口,用户名,密码)

    String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";

    String user = "root";

    String password = "root";

    // 3)建立连接

    conn = DriverManager.getConnection(url, user, password);

    // 4)创建PreparedStatement(动态) Statement(静态的)(拼sql语句)

    st = conn.createStatement();

    // 5)执行数据库命令(crud)

    // executeUpdate insert update delete

    // executeQuery select

    // 查询10号部门的员工的员工编号,员工姓名,部门编号

    rs = st.executeQuery("select empno,ename,deptno from emp where deptno=10");

    // 6)结果的处理

    while (rs.next()) {

    // 双引号里是数据库里的字段名,

    String e_name = rs.getString("ename");

    System.out.println(e_name);

    int emp_no = rs.getInt("empno");

    System.out.println(emp_no);

    int deptno = rs.getInt("deptno");

    System.out.println(deptno);

    }

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    // 7)关闭连接

    try {

    rs.close();

    st.close();

    conn.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    }

    // 删除 delete from 表名 where 字段名=''

    @Test

    public void del() {

    Connection conn = null;

    Statement st = null;

    try {

    // 1)载入jdbc驱动(指定我要连接到哪种数据库,连接不同数据库用不同驱动)

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

    // 2)定义连接url(连接准备 url,端口,用户名,密码)

    String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";

    String user = "root";

    String password = "root";

    // 3)建立连接

    conn = DriverManager.getConnection(url, user, password);

    // 4)创建PreparedStatement(动态) Statement(静态的)(拼sql语句)

    st = conn.createStatement();

    // 5)执行数据库命令(crud)

    // executeUpdate insert update delete

    // executeQuery select

    // 查询10号部门的员工的员工编号,员工姓名,部门编号

    int count = st.executeUpdate("delete from emp_copy where ename='SCOTT'");

    System.out.println(count);

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    // 7)关闭连接

    try {

    st.close();

    conn.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    }

    // 修改 update 表名 set 字段名='' where 字段名=

    @Test

    public void update() throws ClassNotFoundException, SQLException {

    Connection conn = null;

    Statement st = null;

    try {

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

    String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";

    String user = "root";

    String password = "root";

    conn = DriverManager.getConnection(url, user, password);

    st = conn.createStatement();

    int count = st.executeUpdate("update dept_copy set dname='java70' where dname='70'");

    System.out.println(count);

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    try {

    st.close();

    conn.close();

    } catch (SQLException e2) {

    e2.printStackTrace();

    }

    }

    }

    // 添加 insert into 表名(,,) values(,,)

    @Test

    public void insert() throws ClassNotFoundException, SQLException {

    Connection conn = null;

    Statement st = null;

    try {

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

    String url = "jdbc:mysql://localhost:3306/java16?characterEncoding=UTF-8&useUnicode=true";

    String user = "root";

    String password = "root";

    conn = DriverManager.getConnection(url, user, password);

    st = conn.createStatement();

    int count = st.executeUpdate("insert into dept_copy(deptno,dname) values(50,'cx')");

    System.out.println(count);

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    try {

    st.close();

    conn.close();

    } catch (Exception e2) {

    e2.printStackTrace();

    }

    }

    }

    }

    相关文章

      网友评论

          本文标题:jdbc-增删改查

          本文链接:https://www.haomeiwen.com/subject/bgbszftx.html