jdbc-增删改查

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();

}

}

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文内容 1.什么是JDBC以及为什么要使用JDBC 2.JDBC核心API的讲解 3.使用JDBC核心API进行...
    Vincilovfang阅读 1,235评论 0 11
  • JDBC简介 SUN公司为了简化、统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC。JDBC...
    奋斗的老王阅读 1,550评论 0 51
  • 再见了王者荣耀,为了不再年轻的身体,为了需要陪伴的家人,为了正在被窃取的青春,为了拯救那日渐怠惰的灵魂…… ...
    左巴与禅阅读 469评论 0 2
  • 林天拿着《天世》离开了一楼,到藏书阁外时他发现那老人依旧站在那但看自己的眼神有一点不同。“长老您贵姓”林天...
    弑鱼阅读 308评论 0 2
  • 很多时候我们对于生活或是工作或是感情总会有疑惑,我们感觉应该是对的,可是却又总觉得差了一点点,却很难当即找到真正的...
    今天安好阅读 1,065评论 0 0