【java】-- JDBCDemo:连接,简单增删查改

本文档整理了使用Java JDBCDemo进行数据库连接和基本的增删查改操作的方法,适用于环境配置为student数据库的student_table表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


整理起来方便以后使用


环境

数据库:student
数据表:student_table
表结构:
student_table


jdbcdemo.java

package com.JDBCdemo;

import java.sql.*;

/**
*
* @author      : pinsily
* @email       : 13160724868@163.com
* @version     : 0.0.1
* @create_time : 2017年6月5日 上午9:31:46
* 
* @update_time : 2017年6月5日 下午16:26:22  增加 增删查改 功能,方便以后使用
*
*/

public class testJDBC {

    /**
     * 加载驱动
     * */
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接
     * */
    public static Connection getConnection() {

        String url = "jdbc:mysql://localhost:3306/student";
        String username = "root";
        String password = "pinsily";
        Connection con = null;

        try {
            con = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }


    /**
     * 关闭连接
     * */
    public static void close(ResultSet rs, Statement stm, Connection con) {

        try {

            if (rs != null)
                rs.close();

            if (stm != null) {
                stm.close();
            }

            if (con != null) {
                con.close();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 插入数据,用得使用可以把 sql语句 当成参数
     * */
    public void insert() {
        String sql = "insert into student_table (name) values('pinsily')";
        Connection con = getConnection();
        Statement stm = null;
        try {

            stm = con.createStatement();

            //执行 SQL 语句并返回结果
            int result = stm.executeUpdate(sql);

            if (result != 0) {
                System.out.println("操作成功,受影响" + result + "行");
            }

        } catch (SQLException e) {
            System.out.println("操作失败");
        } finally {
            close(null, stm, con);
        }
    }

    /**
     * 删除操作,用得使用可以把 sql语句 当成参数
     * */
    public void delete() {

        String sql = "delete from student_table  where name = 'pinsily'";
        Connection con = getConnection();
        Statement stm = null;

        try {

            stm = con.createStatement();    

            int count = stm.executeUpdate(sql);

            System.out.println("删除 " + count + " 条数据\n");

        } catch (SQLException e) {

            System.out.println("删除数据失败");

        }finally {
            close(null, stm, con);
        }

    }

    /**
     * 查询数据库,用得使用可以把 sql语句 当成参数
     * */
    public void query() {

        String sql = "select * from student_table";
        Connection con = getConnection();
        Statement stm = null;
        ResultSet rs = null;

        try {

            stm = con.createStatement();    

            //执行sql查询语句,返回查询数据的结果集
            rs = stm.executeQuery(sql);

            System.out.println("查询结果为:");

            // 判断是否还有下一个数据
            while (rs.next()) { 

                // 根据字段名获取相应的值
                String name = rs.getString("name");
                int id = rs.getInt("id");

                //输出查到的记录的各个字段的值
                System.out.println(id+":"+name);

            }

        } catch (SQLException e) {
            System.out.println("查询数据失败");

        }finally {

            close(rs, stm, con);

        }
    }


    /**
     * 更新操作,用得使用可以把 sql语句 当成参数
     * */
    public void update() {

        String sql = "update student_table set name='zhuyuanpeng' where id = 3 ";
        Connection con = getConnection();
        Statement stm = null;

        try {

            //创建用于执行sql语句的Statement对象
            stm = con.createStatement();                

             // 执行更新操作的sql语句,返回更新数据的个数
            int count = stm.executeUpdate(sql);                    

            //输出更新操作的处理结果
            System.out.println("表中更新 " + count + " 条数据");   


        } catch (SQLException e) {
            System.out.println("更新数据失败");
        }finally {

            close(null, stm, con);

        }
    }


    /**
     * 测试函数
     * */
    public static void main(String[] args) {

        testJDBC j = new testJDBC();

        //j.insert();
        //j.delete();
        //j.update();
        j.query();

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值