Java操作数据库之JDBC增删改查
1.java连接MySql数据库

代码区域:


|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.oracle.jdbc.demo1;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class JDBCDemo { //四个属性(四个常量的字符串) /* jdbcName url user password */ private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一个类(DriverManeger)四个接口(Connection、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //获得conn就表示获取了数据库的连接 System.out.println("连接数据库成功"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} |
2.在java中向数据库添加数据
第一种方法:添加数据

代码区域:

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package com.oracle.jdbc.demo2;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class JDBCAdd { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一个类(DriverManeger)四个接口(Connection、PreparedStatement、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //增加数据的操作 String name="田雨"; String sex="女"; String sql="insert into person values(null,'"+name+"','"+sex+"')"; PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句 int i=pst.executeUpdate(); //返回成功插入数据的行数 System.out.println("成功添加了"+i+"条记录"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} |
第二中方法:添加数据
代码区域:

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.oracle.jdbc.demo2;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class JDBCAdd2 { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一个类(DriverManeger)四个接口(Connection、PreparedStatement、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //增加数据的操作 String name="田雨2"; String sex="女"; String sql="insert into person values(null,?,?)"; PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句 pst.setString(1, name); //填充第1个问好 pst.setString(2, sex); //填充第2个问好 int i=pst.executeUpdate(); //返回成功插入数据的行数 System.out.println("成功添加了"+i+"条记录"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} |
3.在java中修改数据库的内容

代码区域:


|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package com.oracle.jdbc.demo3;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class JDBCModify { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一个类(DriverManeger)四个接口(Connection、PreparedStatement、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //修改数据的操作 int id=2; String name="王希宝"; String sex="男"; String sql="update person set name=?,sex=? where id=?"; PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句 pst.setString(1, name); //填充第1个问好 pst.setString(2, sex); //填充第2个问好 pst.setInt(3, id); int i=pst.executeUpdate(); //返回成功修改数据的行数 System.out.println("成功修改了"+i+"条记录"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} |
4.在java中删除数据库的内容

代码区域:

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package com.oracle.jdbc.demo4;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class JDBCDel { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一个类(DriverManeger)四个接口(Connection、PreparedStatement、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //删除数据的操作 int id=2; String sql="delete from person where id=?"; PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句 pst.setInt(1, id); int i=pst.executeUpdate(); //返回成功删除数据的行数 System.out.println("成功删除了"+i+"条记录"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} |
5.在java中查看数据库的内容

代码区域:

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package com.oracle.jdbc.demo5;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class JDBCFindAll { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一个类(DriverManeger)四个接口(Connection、PreparedStatement、ResultSet、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //查询数据的操作 String sql="select id,name,sex from person"; PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句 ResultSet rs=pst.executeQuery(); while(rs.next()){ int id=rs.getInt("id"); String name=rs.getString("name"); String sex=rs.getString("sex"); System.out.println(id+" "+name+" "+sex); } } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} |
Java操作数据库之JDBC增删改查的更多相关文章
- MyBatis操作数据库(基本增删改查)
一.准备所需工具(jar包和数据库驱动) 网上搜索下载就可以 二.新建一个Java project 1.将下载好的包导入项目中,build path 2.编写MyBatis配置文件:主要填写prope ...
- DjangoMTV模型之model层——ORM操作数据库(基本增删改查)
Django的数据库相关操作 对象关系映射(英语:(Object Relational Mapping,简称ORM),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换.从效果上说 ...
- c#基础在winform操作数据库,实现增删改查
1.数据库操作类代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...
- mysql 操作数据库创建,增删改查
创建数据库 默认字符编码 默认排序CREATE DATABASE IF NOT EXISTS day11 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; / ...
- java对sql server的增删改查
package Database; import java.sql.*; public class DBUtil { //这里可以设置数据库名称 private final static String ...
- Android(java)学习笔记245:ContentProvider使用(银行数据库创建和增删改查的案例)
1. Android的四大组件: (1)Activity 用户交互的UI界面 (2)Service 后台运行的服务 (3)BroadcastReceiver 广播接收者 (4)ContentPro ...
- Android(java)学习笔记189:ContentProvider使用(银行数据库创建和增删改查的案例)
1. Android的四大组件: (1)Activity 用户交互的UI界面 (2)Service 后台运行的服务 (3)BroadcastReceiver 广播接收者 (4)ContentPro ...
- 使用java对sql server进行增删改查
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- Java操作数据库——使用JDBC连接数据库
Java操作数据库——使用JDBC连接数据库 摘要:本文主要学习了如何使用JDBC连接数据库. 背景 数据持久化 数据持久化就是把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应 ...
随机推荐
- CF682C Alyona and the Tree
题意翻译 题目描述 给你一棵树,边与节点都有权值,根节点为1,现不停删除叶子节点形成新树,问最少删掉几个点,能使得最后剩下的树内,∀v与其子树内∀u间边权的和小于点u权值 输入输出格式 输入格式: 第 ...
- 牛客练习赛51 C 勾股定理
链接:https://ac.nowcoder.com/acm/contest/1083/C 题目描述 给出直角三角形其中一条边的长度n,你的任务是构造剩下的两条边,使这三条边能构成一个直角三角形. 输 ...
- idea配置less自动编译
参考: idea配置less自动编译 1. 电脑安装node.js环境: window下直接上官网下载node.msi文件下载安装即可 安装完成后在命令行执行如下命令表明安装成功 npm -v nod ...
- php读取excel文件并导入数据库(表头任意设定)
最近收到一个很奇葩的需求,要求上传excel员工工资表,表格表头不固定,导入后字段名为表头的拼音,每月导入一次,当月重复导入则覆盖现有的当月表头,并且可以按照在界面上按照月份筛选显示,我写的代码主要包 ...
- 在centos6.4下安装python3.5
1.安装依赖包 ./configure --prefix=/usr/local/python3.5 --enable-shared make && make install yum g ...
- echarts markLine 辅助线非直线设置
效果图: 用例option: option = { title: { text: '未来一周气温变化', subtext: '纯属虚构' }, tooltip: { trigger: 'axis' } ...
- spring boot配置分页插件
在springboot中使用PageHelper插件有两种较为相似的方式,接下来我就将这两种方式进行总结. 方式一:使用原生的PageHelper 1.在pom.xml中引入依赖 <depend ...
- app接口开发
最近一段时间一直在做APP接口,总结一下APP接口开发过程中的注意事项: 1.效率:接口访问速度 APP有别于WEB服务,对服务器端要求是比较严格的,在移动端有限的带宽条件下,要求接口响应速度要快,所 ...
- 简单的物流项目实战,WPF的MVVM设计模式(三)
往Services文件里面添加接口以及实现接口 IUserService接口 List<User> GetAllUser(); GetUserService类 ConnectToDatab ...
- 02MySQL数据库
1.MySQL启动和关闭 2.登录MySQL数据库 MySQL是一个需要账户名密码登录的数据库,登陆后使用,它提供了一个默认的root账号,使用安装时设置的密码即可登录. 格式1:cmd> m ...