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中对数据库进行增删改查的更多相关文章

  1. java程序设计课期中考试——数据库的增删改查和简单的js界面

    首先是设计思路,对于数据库的增删改查,我们借助Ecilipse来进行前端和后端的编写.Ecilipse是可以进行java web项目的操作的. 前端,我们选择用使用jsp,所谓的jsp就是可以嵌入其他 ...

  2. Android中Sqlite数据库进行增删改查

    今天这篇文章写Sqlite数据库,通过一个小案例来完整讲一下数据库常见的CRUD操作. 先对知识点总结: SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHel ...

  3. java连接Oracle数据库实现增删改查并在Navicat中显示

    创建TEST表 eclipse中的java项目 代码 数据库方法类 DBUtil: package util; import java.sql.Connection; import java.sql. ...

  4. django中对数据库的增删改查

    Django的配置文件时settings.py中的 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplate ...

  5. 通过java实现对数据库的增删改查

    package cn.hncu; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet; ...

  6. java实现hbase数据库的增删改查操作(新API)

    操作环境: java版本:    jdk 1.7以上 hbase 版本:1.2.x hadoop版本:2.6.0以上 实现功能: 1,创建指定表 2,删除指定表 3,根据表名,行键,列族,列描述符,值 ...

  7. Java通过JDBC连接数据库的三种方式!!!并对数据库实现增删改查

    前言 java连接数据库完整流程为: 1,获得驱动(driver),数据库连接(url),用户名(username),密码(password)基本信息的三种方式. 2,通过获得的信息完成JDBC实现连 ...

  8. Android SQL语句实现数据库的增删改查

    本文介绍android中的数据库的增删改查 复习sql语法: * 增 insert into info (name,phone) values ('wuyudong','111') * 删 delet ...

  9. TP5.1:数据库的增删改查操作(基于面向对象操作)

    我们现实中对数据库的增删改查操作,都是使用模型类进行操作的(表名::),也就是面向对象操作,只有底层的代码用的是数据库操作(Db::table('表名')) 下面我将贴出模型类进行的增删改查操作,通过 ...

随机推荐

  1. 生成html页面客户端随机数和验证码

    生成随机数: var chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', ...

  2. MVC、MVP、MVVM架构模式

    MVC模式 如何设计一个程序的结构,这是一门专门的学问,叫做"架构模式"(architectural pattern),属于编程的方法论. MVC模式就是架构模式的一种,不仅适用于 ...

  3. django之全文检索

    全文检索 全文检索不同于特定字段的模糊查询,使用全文检索的效率更高,并且能够对于中文进行分词处理 haystack:django的一个包,可以方便地对model里面的内容进行索引.搜索,设计为支持wh ...

  4. SVN版本控制系统最佳实践

    第1章SVN介绍及应用场景 1.1什么是SVN(Subversion) Svn(subversion)是近年来崛起非常优秀的版本管理工具,与CVS管理工具一样,SVN是一个跨平台的开源的版本控制系统. ...

  5. nrm操作

    nrm操作 nrm use cnpm // 选择镜像nrm ls //查看镜像

  6. SignalR web实时同步 消息推送 广播

    源码:https://github.com/SignalR/SignalR demo:http://download.csdn.net/download/qq_21533697/9702791#com ...

  7. CBCentralManager Class 的相关分析

    Overview 总体概述 CBCentralManager objects are used to manage discovered or connected remote peripheral ...

  8. struts2 action重定向action中文乱码处理

    比如:Action方法productCategorySave()变量message,传递给Action方法productCategoryAdd(),当变量message为中文变量时,要进行编码设置,不 ...

  9. MySQL数据库篇之pymysql模块的使用

    主要内容: 一.pymysql模块的使用 二.pymysq模块增删改查 1️⃣  pymsql模块的使用 1.前言:之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库, 那如何在p ...

  10. WWW.LoadFromCacheOrDownload

    [WWW.LoadFromCacheOrDownload] static WWWLoadFromCacheOrDownload(string url, int version, uint crc = ...