【java】-- JDBCDemo:连接,简单增删查改
来源:互联网 发布:风量流量计软件专利 编辑:程序博客网 时间:2024/06/07 02:02
整理起来方便以后使用
环境
数据库:student
数据表: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(); }}
阅读全文
0 0
- 【java】-- JDBCDemo:连接,简单增删查改
- java连接数据库的增删改查
- java连接数据库增删查改
- Java连接MySQL实现增删改查
- java连接数据库,进行增删改查
- Java 连接 Mongodb 增删改查操作
- Java连接数据库--增删改查
- Java-Hibernate简单增删改查
- java Mysql简单增删改查
- java连接mysql与简单操作mysql的增删改查简单例子
- ASP.NET 连接数据库 增删改查简单实例
- ASP.NET 连接数据库 增删改查简单实例
- python2连接数据库进行简单的增删改查
- 简单yii增删改查
- ThinkPHP简单增删改查
- mongodb-简单增删改查
- yii简单增删改查
- Hibernate简单增删改查
- [笔记]: 树链剖分
- IO流的一些知识点
- uva10474
- 0.masm32-搭建环境和MessageBoxA弹框
- python 入门
- 【java】-- JDBCDemo:连接,简单增删查改
- Android 之 ListView 的焦点问题
- Spring Boot属性配置文件详解
- Linux常用命令大全
- 前后端分离项目的技术与框架通用方案
- jQuery全选全不选第二次失效问题解决办法
- 好好编程,努力写论文,早日毕业
- VIM+插件构建源码分析工具
- php的中文正则表达式