栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java课程设计(JDBC)全程笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java课程设计(JDBC)全程笔记

一、准备工作 1.netstat -ano | findstr 3306 :检查3306端口是否被启动 2.services.msc 开启服务 3.打开idea(2021.3.1)关闭现在的项目,点击file→new java project新建java项目 4.java项目选用jdk版本→jdk1.8 二、项目开始 5.项目名:java_keshe_0515 建包 建java类 6.创建file header


7.navicat连接MySQL数据库 或 使用cmd 的 mysql -u root -p 启动MySQL数据库 8.navicat新建连接 localhost 9.Java连接MySQL JDBC(Java Database Connectivity)

JDBC是Java提供的操作数据库的接口

会用到如下命令

  1. connection连接对象

  2. statement操作数据库,发送sql命令

  3. resultset result是结果 set是集合 从数据库中查询的数据

10.java连接数据库要用到各数据库厂商的驱动包(jar包)

在项目目录新建名为lib的文件夹 将jar包导入到该文件夹中。

idea中找到jar包,右键将jar包添加到库

11.创建一个院系类College

创建一个院系类College到com.tyu下

院系类属于POJO(只有简单的属性和set get方法。不处理业务,没有业务逻辑)

属性

  1. id 编号 int型

  2. name 名称 String型

(以上属性定义为private,通过set和get方法来访问该私有属性)

12.写出college类的私有属性的set()和get()方法

使用快捷键 alt+insert 或鼠标右键通过idea提供的自编辑功能写出college类的私有属性的set()和get()方法

13.建立一个名为uollab的数据库

通过navicat建立一个名为uollab的数据库(字符集选用utf-8)。

然后创建表college

表中字段:

  1. id int 主键,自增
  2. name varchar(20) 可变长的字符串类型的字段,最长为20个字符。
14.创建Uolab类用于连接数据库

在idea中创建一个名为Uolab的类用于连接13中创建好的数据库。

在类中写出连接数据库的相关代码用于加载连接数据库

public class Uolab {
    public static void main(String[] args) {
        // 定义一个连接对象
        Connection conn = null;
        // 连接数据库
        // 加载驱动类,mysql的驱动类(位于MySQL的jar包中)
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("加载成功");
            // 指定数据库的连接字符串和用户名密码,建立连接
            // 连接字符串,指定数据库名、服务器名、服务器端口
            // 数据库类型
            String url="jdbc:mysql://localhost:3306?uolab";
            String username = "root";
            String pwd = "123456";// 根据实际填写
            conn = DriverManager.getConnection(url,username,pwd);
            System.out.println("连接成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
三、增删改查 15.(新增)

将该类封装成类方法以便其他对象调用

将上述14中的代码装载到新建类Dbutil中,在类中建立静态方法。

public static Connection getConnection()
package com.tyu;


import java.sql.*;


public class Dbutil {
    
    public static Connection getConnection(){
        // 定义一个连接对象
        Connection conn = null;
        // 连接数据库
        // 加载驱动类,mysql的驱动类(位于MySQL的jar包中)
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("加载成功");
            // 指定数据库的连接字符串和用户名密码,建立连接
            // 连接字符串,指定数据库名、服务器名、服务器端口
            // 数据库类型
            String url="jdbc:mysql://localhost:3306/uolab";
            String username = "root";
            String pwd = "123456";// 根据实际填写
            conn = DriverManager.getConnection(url,username,pwd);
            System.out.println("连接成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 返回连接对象,给其他程序使用
        return conn;
    }

    
    public static void release(Connection conn, Statement stmt, ResultSet rs){
        try {
            if (conn != null) {
                conn.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

然后即可在类Uolab的主方法中调用该方法连接数据库,使用数据库。

16.避免插入数据的乱码

将如下代码(位于Dbutil类中)

String url="jdbc:mysql://localhost:3306/uolab";

改成

String url="jdbc:mysql://localhost:3306/uolab?characterEncoding=utf8";
17.将查询封装成方法

将位于Uolab类中的主方法中的插入数据代码封装成名为insert的静态方法存放于Uolab类中。便于使用。

public class Uolab {
    public static void main(String[] args) {
        College college = new College();
        college.setName("计算机科学与技术");
        int result = Uolab.insert(college);
        System.out.println("result=" + result);
    }
    
    private static int insert(Object obj) {
        Connection conn = Dbutil.getConnection();
        // 增加记录
        Statement stmt = null;
        int result = 0;
        try {
            stmt = conn.createStatement();

            String sql = null;
            if (obj instanceof College) {
                //将obj对象向下转型为college对象
                College college = (College) obj;
                //如果obj是院系对象
                sql = "insert into college(name) values('" + college.getName() + "')";
                System.out.println("sql=" + sql);
            }

            // 向数据库发送一个sql命令,增加一条记录,方法返回一个数字
            stmt.executeUpdate(sql);
            result = stmt.executeUpdate(sql);
            if (result > 0) {
                System.out.println("增加成功");
            } else {
                System.out.println("增加失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Dbutil.release(conn, stmt, null);
        }
        return result;
    }
}
18.(更改)

如法炮制,修改17中的代码,将插入功能修改为更新功能。在main中测试

public class Uolab {
    public static void main(String[] args) {
        College college = new College();
        college.setId(1);
        college.setName("体育系");
        int result = Uolab.update(college);
        System.out.println("result=" + result);
    }
    
    private static int update(Object obj) {
        Connection conn = Dbutil.getConnection();
        // 修改记录
        Statement stmt = null;
        int result = 0;
        try {
            stmt = conn.createStatement();
            String sql = null;
            if (obj instanceof College) {
                //将obj对象向下转型为college对象
                College college = (College) obj;
                //如果obj是院系对象
                sql = "update college set name ='" + college.getName()
                        + "' where id="+college.getId();
                System.out.println("sql=" + sql);
            }
            // 向数据库发送一个sql命令,修改一条记录,方法返回一个数字
            stmt.executeUpdate(sql);
            result = stmt.executeUpdate(sql);
            if (result > 0) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Dbutil.release(conn, stmt, null);
        }
        return result;
    }
}
19.(删除)

只需执行删除操作

private static int delete(Object obj) {
    Connection conn = Dbutil.getConnection();
    // 删除记录
    Statement stmt = null;
    int result = 0;
    try {
        stmt = conn.createStatement();
        String sql = null;
        if (obj instanceof College) {
            //将obj对象向下转型为college对象
            College college = (College) obj;
            //如果obj是院系对象
            sql ="delete from college where id="+college.getId();
            System.out.println("sql=" + sql);
        }
        // 向数据库发送一个sql命令,删除一条记录,方法返回一个数字
        stmt.executeUpdate(sql);
        result = stmt.executeUpdate(sql);
        if (result > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        Dbutil.release(conn, stmt, null);
    }
    return result;
}

测试

public static void main(String[] args) {
        College college = new College();
        college.setId(2);
        int result = Uolab.delete(college);
        college = Uolab.selectCollege(3);
        System.out.println("result" + result);
    }
20.(查询)

实现查询功能需要定义四个变量

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
College college = null;

在方法中分别使用sql语句查询出结果后在赋值给上述四个变量。

private static College selectCollege(int id) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    College college = null;
    try {
        conn = Dbutil.getConnection();
        String sql = "select id,name from college where id=" + id;
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
        rs.next();//打开查询结果集,否则报错
        id = rs.getInt("id");
        String name = rs.getString("name");
        college = new College();
        college.setId(id);
        college.setName(name);
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        Dbutil.release(conn, stmt, rs);
    }
    return college;
}

测试

public static void main(String[] args) {
        College college = new College();
        college.setId(2);
        college = Uolab.selectCollege(3);
        System.out.println("college=" + college);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/888662.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号