Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03.JDBC Statement(2)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
• Update
TestStatement.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.SQLException;    
import java.sql.Statement;    

public  class TestStatement {    
         public  static  void getStatement(){    
                Connection conn =  new ConnectionUtil().openConnection();    
                 try {    
                        Statement stmt = conn.createStatement();    
                        System.out.println(stmt);    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                }    
        }    
         public  static  void createTable(){    
                 //DDL数据定义语句    
                Connection conn =  new ConnectionUtil().openConnection();    
                String sql =  "create table CustomerTbl(id int primary key auto_increment,name varchar(20),email varchar(20))";    
                 try {    
                        Statement stmt = conn.createStatement();    
                         //执行SQL语句    
                        stmt.execute(sql);    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                } finally{    
                         if(conn!= null)    
                                 try {    
                                        conn.close();    
                                }  catch (SQLException e) {    
                                        conn =  null;    
                                        e.printStackTrace();    
                                }    
                }    
        }    
         //DML数据操作语句--CRUD:create、retrive、update、delete    
         public  static  void testUpdate(){    
                 //DDL数据定义语句    
                Connection conn =  new ConnectionUtil().openConnection();    
                String sql =  "update CustomerTbl set name='Redking' where id=1";    
                 try {    
                        Statement stmt = conn.createStatement();    
                         //执行SQL语句    
                        stmt.executeUpdate(sql);    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                } finally{    
                         if(conn!= null)    
                                 try {    
                                        conn.close();    
                                }  catch (SQLException e) {    
                                        conn =  null;    
                                        e.printStackTrace();    
                                }    
                }    
        }    

image
Main.java
package com.michael.main;    

import com.michael.jdbc.ConnectionUtil;    
import com.michael.jdbc.TestStatement;    

public  class Main {    

         /**    
         * @param args    
         */
    
         public  static  void main(String[] args) {    
                ConnectionUtil cu =  new ConnectionUtil();    
                 //第一种方法    
                System.out.println( "第一种方法:"+cu.getConnection());    
                 //第二种方法    
                System.out.println( "第二种方法:"+cu.getConnection( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin"));    
                //第三种方法    
                System.out.println("第三种方法:"+cu.openConnection());    
                TestStatement.getStatement();    
                //TestStatement.createTable();    
                TestStatement.testUpdate();    
        }    


image
测试结果:
image 
• delete
TestStatement.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.SQLException;    
import java.sql.Statement;    

public  class TestStatement {    
         public  static  void getStatement(){    
                Connection conn =  new ConnectionUtil().openConnection();    
                 try {    
                        Statement stmt = conn.createStatement();    
                        System.out.println(stmt);    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                }    
        }    
         public  static  void createTable(){    
                 //DDL数据定义语句    
                Connection conn =  new ConnectionUtil().openConnection();    
                String sql =  "create table CustomerTbl(id int primary key auto_increment,name varchar(20),email varchar(20))";    
                 try {    
                        Statement stmt = conn.createStatement();    
                         //执行SQL语句    
                        stmt.execute(sql);    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                } finally{    
                         if(conn!= null)    
                                 try {    
                                        conn.close();    
                                }  catch (SQLException e) {    
                                        conn =  null;    
                                        e.printStackTrace();    
                                }    
                }    
        }    
         //DML数据操作语句--CRUD:create、retrive、update、delete    
         public  static  void testDelete(){    
                 //DDL数据定义语句    
                Connection conn =  new ConnectionUtil().openConnection();    
                String sql =  "delete from CustomerTbl";    
                 try {    
                        Statement stmt = conn.createStatement();    
                         //执行SQL语句    
                        stmt.executeUpdate(sql);    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                } finally{    
                         if(conn!= null)    
                                 try {    
                                        conn.close();    
                                }  catch (SQLException e) {    
                                        conn =  null;    
                                        e.printStackTrace();    
                                }    
                }    
        }    

image
Main.java
package com.michael.main;    

import com.michael.jdbc.ConnectionUtil;    
import com.michael.jdbc.TestStatement;    

public  class Main {    

         /**    
         * @param args    
         */
    
         public  static  void main(String[] args) {    
                ConnectionUtil cu =  new ConnectionUtil();    
                 //第一种方法    
                System.out.println( "第一种方法:"+cu.getConnection());    
                 //第二种方法    
                System.out.println( "第二种方法:"+cu.getConnection( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin"));    
                //第三种方法    
                System.out.println("第三种方法:"+cu.openConnection());    
                TestStatement.getStatement();    
                //TestStatement.createTable();    
                //TestStatement.testInsert();    
                //TestStatement.testUpdate();    
                TestStatement.testDelete();    
        }    


image
测试结果:
image
增删改三个操作都是使用的executeUpdate()方法
• 使用Statement 执行DML 
–查询 
TestStatement.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.ResultSet;    
import java.sql.SQLException;    
import java.sql.Statement;    

public  class TestStatement {    
         public  static  void getStatement(){    
                Connection conn =  new ConnectionUtil().openConnection();    
                 try {    
                        Statement stmt = conn.createStatement();    
                        System.out.println(stmt);    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                }    
        }    
         public  static  void createTable(){    
                 //DDL数据定义语句    
                Connection conn =  new ConnectionUtil().openConnection();    
                String sql =  "create table CustomerTbl(id int primary key auto_increment,name varchar(20),email varchar(20))";    
                 try {    
                        Statement stmt = conn.createStatement();    
                         //执行SQL语句    
                        stmt.execute(sql);    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                } finally{    
                         if(conn!= null)    
                                 try {    
                                        conn.close();    
                                }  catch (SQLException e) {    
                                        conn =  null;    
                                        e.printStackTrace();    
                                }    
                }    
        }    
         //DML数据操作语句--CRUD:create、retrive、update、delete    
         public  static  void testQuery(){    
                 //DDL数据定义语句    
                Connection conn =  new ConnectionUtil().openConnection();    
                String sql =  "select * from CustomerTbl";    
                 try {    
                        Statement stmt = conn.createStatement();    
                         //执行SQL语句    
                        ResultSet rs = stmt.executeQuery(sql);    
                         while(rs.next()){    
                                 //可以通过列索引    
                                 int id = rs.getInt(1);    
                                 //可以通过列名称    
                                String name = rs.getString( "name");    
                                String email = rs.getString(3);    
                                System.out.println(id+ ":"+name+ ":"+email);    
                        }    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                } finally{    
                         if(conn!= null)    
                                 try {    
                                        conn.close();    
                                }  catch (SQLException e) {    
                                        conn =  null;    
                                        e.printStackTrace();    
                                }    
                }    
        }    

image
Main.java 
package com.michael.main;    

import com.michael.jdbc.ConnectionUtil;    
import com.michael.jdbc.TestStatement;    

public  class Main {    

         /**    
         * @param args    
         */
    
         public  static  void main(String[] args) {    
                ConnectionUtil cu =  new ConnectionUtil();    
                 //第一种方法    
                System.out.println( "第一种方法:"+cu.getConnection());    
                 //第二种方法    
                System.out.println( "第二种方法:"+cu.getConnection( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin"));    
                //第三种方法    
                System.out.println("第三种方法:"+cu.openConnection());    
                TestStatement.getStatement();    
                //TestStatement.createTable();    
                //TestStatement.testInsert();    
                //TestStatement.testUpdate();    
                //TestStatement.testDelete();    
                TestStatement.testQuery();    
        }    


image 
测试结果:
先在数据库中添加几个数据,我们只测试下查询效果哈~
image
image
#################Michael分割线#########################
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发技术讨论技术圈: [url]http://g.51cto.com/itedu[/url]
#################Michael分割线#########################








本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/151714,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
23天前
|
Java
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
19 1
|
23天前
|
Java
排课系统【JSP+Servlet+JavaBean】(Java课设)
排课系统【JSP+Servlet+JavaBean】(Java课设)
7 0
|
23天前
|
Java
仓库管理系统【JSP+Servlet+JavaBean】(Java课设)
仓库管理系统【JSP+Servlet+JavaBean】(Java课设)
13 0
|
23天前
|
Java
学校人员管理系统【JSP+Servlet+JavaBean】(Java课设)
学校人员管理系统【JSP+Servlet+JavaBean】(Java课设)
16 0
|
20天前
|
监控 JavaScript 前端开发
《理解 WebSocket:Java Web 开发的实时通信技术》
【4月更文挑战第4天】WebSocket是Java Web实时通信的关键技术,提供双向持久连接,实现低延迟、高效率的实时交互。适用于聊天应用、在线游戏、数据监控和即时通知。开发涉及服务器端实现、客户端连接及数据协议定义,注意安全、错误处理、性能和兼容性。随着实时应用需求增加,WebSocket在Java Web开发中的地位将更加重要。
|
1月前
|
Web App开发 前端开发 开发工具
介绍Web开发的基础知识
介绍Web开发的基础知识
29 7
|
1月前
|
缓存 关系型数据库 API
后端开发:构建高效、可扩展的Web应用程序的关键
后端开发:构建高效、可扩展的Web应用程序的关键
21 0
|
6天前
|
JSON Java fastjson
Spring Boot 底层级探索系列 04 - Web 开发(2)
Spring Boot 底层级探索系列 04 - Web 开发(2)
15 0
|
6天前
|
安全 编译器 PHP
PHP 8.1版本发布:引领Web开发新潮流
PHP编程语言一直是Web开发的主力军,而最新发布的PHP 8.1版本则为开发者们带来了更多创新和便利。本文将介绍PHP 8.1版本的主要特性,包括更快的性能、新的语言功能和增强的安全性,以及如何利用这些功能来提升Web应用程序的质量和效率。
|
9天前
|
PHP
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册