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连接数据库. 背景 数据持久化 数据持久化就是把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应 ...
随机推荐
- uboot初识
一. 什么是uboot 1.1. uboot的由来 1.1.1. uboot是SourceForge上的开源项目 1.1.2. uboot就是由一个人发起,然后由整个网络上所有感兴趣的人共同维护发展而 ...
- Scrapy 教程(六)-反爬
伪装浏览器 服务器可以查看访问的终端,如果不是浏览器,可能会被屏蔽,而且即使你用同一浏览器访问频率过快,也可能被屏蔽,所以需要伪装浏览器反爬. 有以下几种方法 1. 在 settings中添加 use ...
- vue.js学习记录
vue.js学习记录 文章已同步我的github笔记https://github.com/ymblog/blog,欢迎大家加star~~ vue实例 生命周期 beforeCreate:不能访问thi ...
- Hive常用非交互式命令
[hadoop@hadoop hive-0.13.1]$ bin/hive -help usage: hive -d,--define <key=value> Variable subsi ...
- vue编写轮播图组件
<template> <div id="slider"> <div class="window" @mouseover=& ...
- 46. Permutations (JAVA)
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- [转]走近0day
首先,需要大家端正一下学习态度-也就是对于破解的态度.每一个有一定修为的软件破解者,也就是CRACKER,都很清楚,我们破解掉软件的序列号,功能限制,时间限制等等东西都不是最终的目的,一个真正的CRA ...
- Provider增删改查
package com.fei.provider; import org.apache.ibatis.jdbc.SQL; import com.fei.domain.User; public clas ...
- MySQL基础day03 存储引擎和外键MySQL 5.6
MySQL基础day03_存储引擎和外键-MySQL 5.6 外键的条件: 1,表的存储引擎为innodb存储引擎 2,表中外键字段的类型要与参考表的字段类型一致 3,外键字段要是索引类型中的一种 M ...
- 关于win7虚拟机的安装
VMware 安装以及秘钥 win7的光盘文件