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

本文通过示例代码展示了如何使用Java JDBC进行数据库的基本操作,包括创建表、插入、更新、删除及查询数据等。
• 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分割线#########################
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值