//在jdbc中进行增删查改

//查看所有

public static void findAll() {
String url = "jdbc:mysql://localhost:3306/epet";//加载驱动器
String user = "root";
String password = "root";
String sql = "SELECT * FROM dog";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);//加载JDBC驱动器
statement = connection.createStatement();//与数据库建立连接
resultSet = statement.executeQuery(sql);//发送SQL语句 ,并且返回结果
while(resultSet.next()){//处理返回结果
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getInt("health"));
System.out.println(resultSet.getInt("love"));
System.out.println(resultSet.getObject(5));
System.out.println("=================");
}
} catch (Exception e) {
// TODO: handle exception
}finally{
try { //关闭资源
if (null != resultSet) {
resultSet.close();
}
if (null != statement) {
statement.close();
}
if (null != connection) {
connection.close();
}
} catch (Exception e2) {
// TODO: handle exception
}

}
}

//插入语句
public static void insert() {
String url = "jdbc:mysql://localhost:3306/epet";
String user = "root";
String password = "root";
String sql = "INSERT INTO dog(name,health,love,strain) VALUES ('aaa',90,100, 'bbb')";
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
connection = DriverManager.getConnection(url, user, password);

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement = connection.createStatement();
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//更新数据库
public static void update() {
String url = "jdbc:mysql://localhost:3306/epet";
String user = "root";
String password = "root";
String sql = "UPDATE dog SET name='haha' WHERE id=1";
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement = connection.createStatement();
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//删除数据库元素
public static void delete() {
Connection connection = null;
Statement statement = null;

String url = "jdbc:mysql://localhost:3306/epet";
String user = "root";
String password = "root";
String sql = "DELETE FROM dog WHERE id =1";
String sql2 = "DELETE FROM dog WHERE id =2";
try {
Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement = connection.createStatement();
statement.executeUpdate(sql);
statement.executeUpdate(sql2);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

jdbc的实例应用:增删查改实现的更多相关文章

  1. 2015.8.2 jdbc实现商品类的增删查改

    在惠普济宁基地进行了两周sql和java的学习,学到很多东西 刚才实现了用jdbc访问数据库对数据库进行操作,是用eclipse写的,过几天移植到NetBeans上,个人还是比较习惯看图形化界面 前几 ...

  2. hibernate基础增删查改简单实例

    hibernate 基础理论知识网上很多,可以百度和google.这里不做多的介绍,以一个User表来开展例子 建一个web-project 我这里用了junit单元测试环境来进行增删查改的测试,别的 ...

  3. backbonejs mvc框架的增删查改实例

    一:开发环境 coffeescript和nodejs需要先安装,没装网上自己查安装步骤. 代码编写环境及esp框架下载: esp框架下载地址:https://github.com/nonocast/e ...

  4. JDBC+Servlet+jsp(增删查改)

    先在mysql新增数据库和表先,把下面的几句代码复制去到mysql运行就可以创建成功了!  创建数据库 create database jdbc01 character set utf8 collat ...

  5. SSH2 增删查改实例

    (一)引入包 (共73个,不一定都需要,但是我的项目是这么多,经过调试,没有包冲突) (二)创建数据库表 建立数据库octtest,并创建user表,表里面一共4个字段:id,姓,名,年龄. 语句如下 ...

  6. JDBC终章- 使用 DBUtils实现增删查改- C3P0Utils数据源/QueryRunner runner连接数据源并执行sql

    JDBC终章- 使用 DBUtils实现增删查改 1.数据库结构 Create Table CREATE TABLE `user` ( `id` ) NOT NULL AUTO_INCREMENT, ...

  7. [课本]JDBC课程6--使用JDBC的DAO模块化--完成数据库的增删查改_工具类JDBCTools四个(Preparedstatement)功能模块的敲定版

    (课本P273-任务九) /**DAO: Data Access Object * 为什么用: 实现功能的模块化,更有利于代码的维护和升级 * 是什么: 访问数据信息的类,包含对数据的CRUD(cre ...

  8. 在Eclipse上实现简单的JDBC增删查改操作

    在Javaweb的学习里,学到了如何完成简单的增删查改操作,在这里撰写一篇文章以便自己整理回忆. 首先要建立一些包和导入一些文件.建一些类.具体框架如图  编写Product类 public clas ...

  9. Java连接MySQL数据库及简单的增删查改操作

    主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...

随机推荐

  1. selenium python的使用(二)

    1.selenium获取到的信息是 把页面加载完毕之后 获取异步加载的html源码 html=driver.find_element_by_xpath("/html").get_a ...

  2. python写2048小游戏

    #!/usr/bin/env python # coding=utf-8 #******************************************************** # > ...

  3. grafana + influxdb + telegraf , 构建性能监控平台

    1.安装平台 1).grafana , 访问各类数据源 , 自定义报表.显示图表等等 , 用于提供界面监控 , 默认端口为3000 , 默认登陆信息admin wget https://grafana ...

  4. 【vue.js权威指南】读书笔记(第二章)

    [第2章:数据绑定] 何为数据绑定?答曰:数据绑定就是将数据和视图相关联,当数据发生变化的时候,可以自动的来更新视图. 数据绑定的语法主要分为以下几个部分: 文本插值:文本插值可以说是最基本的形式了. ...

  5. [转]搬瓦工换机房换ip之后不能连外网

    搬瓦工换机房换ip之后不能连外网 时间 2015-07-21 15:17:16  Wendal随笔 原文  http://wendal.net/2015/07/21.html 主题 iptables ...

  6. 在Swift项目中使用cocoaPods导入第三方OC库

    首先保证你的项目是基于cocoaPods的,并且是通过XX.xcworkspace打开的.cocoaPods安装教程(Xcode6以上) 下面就第三方库MBProgressHUD来讲解如何在Swift ...

  7. 让超出父视图范围的子视图响应事件,在UIView范围外响应点击

    /** *  在父视图中重写该方法,这样可使超出部分响应事件. */ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {  ...

  8. Android下安装应用不成功解决

    在手机上安装应用程序不成功,可以尝试把手机连接电脑,然后使用adb进行安装,adb安装命令: adb install Android_65632.apk 当出现: success!就表示成功,但当不成 ...

  9. rplidar & hector slam without odometry

    接上一篇:1.rplidar测试 方式一:测试使用rplidar A2跑一下手持的hector slam,参考文章:用hector mapping构建地图 但是roslaunch exbotxi_br ...

  10. oracle指令

    删除用户和用户下所有的表: drop user user_name cascade; 导入数据库: cd /home/oracle/app/admin/orcl/dpdump impdp   dire ...