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增删改查的更多相关文章

  1. MyBatis操作数据库(基本增删改查)

    一.准备所需工具(jar包和数据库驱动) 网上搜索下载就可以 二.新建一个Java project 1.将下载好的包导入项目中,build path 2.编写MyBatis配置文件:主要填写prope ...

  2. DjangoMTV模型之model层——ORM操作数据库(基本增删改查)

    Django的数据库相关操作 对象关系映射(英语:(Object Relational Mapping,简称ORM),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换.从效果上说 ...

  3. c#基础在winform操作数据库,实现增删改查

    1.数据库操作类代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...

  4. mysql 操作数据库创建,增删改查

    创建数据库 默认字符编码 默认排序CREATE DATABASE IF NOT EXISTS day11 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; / ...

  5. java对sql server的增删改查

    package Database; import java.sql.*; public class DBUtil { //这里可以设置数据库名称 private final static String ...

  6. Android(java)学习笔记245:ContentProvider使用(银行数据库创建和增删改查的案例)

    1. Android的四大组件: (1)Activity  用户交互的UI界面 (2)Service  后台运行的服务 (3)BroadcastReceiver 广播接收者 (4)ContentPro ...

  7. Android(java)学习笔记189:ContentProvider使用(银行数据库创建和增删改查的案例)

    1. Android的四大组件: (1)Activity  用户交互的UI界面 (2)Service  后台运行的服务 (3)BroadcastReceiver 广播接收者 (4)ContentPro ...

  8. 使用java对sql server进行增删改查

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  9. Java操作数据库——使用JDBC连接数据库

    Java操作数据库——使用JDBC连接数据库 摘要:本文主要学习了如何使用JDBC连接数据库. 背景 数据持久化 数据持久化就是把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应 ...

随机推荐

  1. linux-memcache安装及memcached memcache扩展

    linux memcached安装yum -y install libevent libevent-deve yum list memcached yum -y install memcached m ...

  2. Nginx工作机制

    Nginx分为单工作进程和多工作进程两种模式.通常采用1个master+多个worker进程配合异步非阻塞的工作机制.master进程主要负责管理自身和下属的worker进程,worker负责处理请求 ...

  3. 码云与Git的使用

    码云注册和使用 网址:https://gitee.com 注册之后新建一个仓库 接下来安装Git 协同开发Git安装与使用 下载地址:https://gitforwindows.org 安装完成之后选 ...

  4. 基于 Redux + Redux Persist 进行状态管理的 Flutter 应用示例

    好久没在 SegmentFault 写东西,唉,也不知道 是忙还是懒,以后有时间 再慢慢写起来吧,最近开始学点新东西,有的写了,个人博客跟这里同步. 一直都在自己的 React Native 应用中使 ...

  5. 网络爬虫之JSOUP

    JSOUP中文文档:http://www.open-open.com/jsoup/推荐博客:http://www.cnblogs.com/jycboy/p/jsoupdoc.html 从一个URL加载 ...

  6. Linux 设置定时清除buff/cache的脚本

    Linux 设置定时清除buff/cache的脚本 查看内存缓存状态 [root@heyong ~]# free -m total used free shared buff/cache availa ...

  7. 基于TCP/UDP协议的socket

    基于TCP协议的socket tcp是基于链接的,必须先启动服务端,然后再启动客户端去链接服务端 server端 import socket sk = socket.socket() sk.bind( ...

  8. 图解Qt安装(Linux平台)

    http://c.biancheng.net/view/3886.html Linux 发行版虽然众多,但 Qt 安装过程大同小异,本节以 CentOS 7 为例来演示 Qt 的安装. 在<Qt ...

  9. Protobuf(一)——Protobuf简介

    Protobuf简介 ​ 什么是 Google Protocol Buffer? 假如您在网上搜索,应该会得到类似这样的文字介绍: ​ Google Protocol Buffer( 简称 Proto ...

  10. Schedule HDU - 6180 (multiset , 贪心)

    There are N schedules, the i-th schedule has start time si and end time ei (1 <= i <= N). Ther ...