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

开发者社区> 技术小美> 正文

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

简介:
+关注继续查看
• 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,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
“飞天加速计划·高校学生在家实践”免费云服务器使用感想
通过这次的云服务器ECS的使用,我收获颇丰,第一次将项目放到了服务器上进行访问,通过阿里云ECS这个云服务器的使用让我更容易认识到云服务器的优势,云服务器ECS远远超过与虚拟机,拥有了一台云服务器后,我不禁想到如何才能够利用好这台服务器,给我的生活带来更多便捷和乐趣。带着这个目标,我开始广泛地了解服务器的原理和应用。在计算机网络世界中,相比个人电脑,服务器发挥了更加关键的作用,它响应用户们的请求并进行处理,为丰富多彩的互联网默默付出,贡献着自己的资源。阿里云ECS云服务器,相当于一个远程电脑,较于本地来说,可以方便我们处理更多的事请,对于我们来说不仅仅是时间上的节省,更多的是本地内存的大大优化
33 0
ECS使用体验
我进行了两周的ECS云服务器的使用
23 0
ECS使用体验
初次使用ECS,成功部署一个基于flask框架的新闻聚合项目在云服务器上。
21 0
创业公司CTO谈创业公司技术选型
创业公司CTO谈创业公司技术选型
5 0
DevOps中开发的作用和主动性
DevOps中开发的作用和主动性
4 0
Redis Cluster 宕机引发的事故(上)
Redis Cluster 宕机引发的事故(上)导读: Redis官方号称支持并发11万读操作,并发8万写操作。由于优异的性能和方便的操作,相信很多人都在项目中都使用了Redis,为了不让应用过分的依赖 Redis服务,Redis的作用只作为提升应用并发和降低应用响应时间存在,即使Redis出现异常,应用程序也不应该出现提供服务失败问题,对此拍拍信最近安排了一次全环境的Redis Cluster 宕机演练。 本文作者系拍拍信架构负责人朱荣松和拍拍信架构开发工程师许彬,授权“技术锁话”进行发布。
3 0
+关注
6906
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载