JavaEEWEB工程师培训-JDBC+Servlet+JSP整合开发之03.JDBCStatement(1)上季我们建立了与数据库的连接,本季我们将对数据库进行操作使用Statement执行DDL、使用Statement执行DML,常用的插入、更新、删除及查询数据哈~•Statement简介–Statement提供了一个操作数据库语句的功能,可通过它来创建表、插入记录、修改记录、删除记录等操作•获得Statement–可以从数据库连接Connection中获得StatementConnectionconn=newConnectionUtil().getConnection();Statementstmt=conn.createStatement();ConnectionUtil.javapackagecom.michael.jdbc;importjava.sql.Connection;importjava.sql.DriverManager;importjava.util.Properties;publicclassConnectionUtil{//第一种方法publicConnectiongetConnection(){Connectionconn=null;try{//Class.forName加载驱动Class.forName("com.mysql.jdbc.Driver");//DriverManager获得连接conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin");returnconn;}catch(Exceptione){e.printStackTrace();}returnnull;}//第二种方法publicConnectiongetConnection(Stringdriver,Stringurl,Stringuser,Stringpassword){Connectionconn=null;try{//Class.forName加载驱动Class.forName(driver);//DriverManager获得连接conn=DriverManager.getConnection(url,user,password);returnconn;}catch(Exceptione){e.printStackTrace();}returnnull;}//第三种方法publicConnectionopenConnection(){Stringdriver="";Stringurl="";Stringuser="";Stringpassword="";Propertiesprop=newProperties();Connectionconn=null;try{//加载属性文件prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));driver=prop.getProperty("driver");url=prop.getProperty("url");user=prop.getProperty("user");password=prop.getProperty("password");//Class.forName加载驱动Class.forName(driver);//DriverManager获得连接conn=DriverManager.getConnection(url,user,password);returnconn;}catch(Exceptione){e.printStackTrace();}returnnull;}}TestStatement.javapackagecom.michael.jdbc;importjava.sql.Connection;importjava.sql.SQLException;importjava.sql.Statement;publicclassTestStatement{publicstaticvoidgetStatement(){Connectionconn=newConnectionUtil().openConnection();try{Statementstmt=conn.createStatement();System.out.println(stmt);}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}Main.javapackagecom.michael.main;importcom.michael.jdbc.ConnectionUtil;importcom.michael.jdbc.TestStatement;publicclassMain{/***@paramargs*/publicstaticvoidmain(String[]args){ConnectionUtilcu=newConnectionUtil();//第一种方法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();}}测试结果:•使用Statement执行DDL–可以使用Statement来执行一个数据定义语句,例如:创建一张表TestStatement.javapackagecom.michael.jdbc;importjava.sql.Connection;importjava.sql.SQLException;importjava.sql.Statement;publicclassTestStatement{publicstaticvoidgetStatement(){Connectionconn=newConnectionUtil().openConnection();try{Statementstmt=conn.createStatement();System.out.println(stmt);}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}publicstaticvoidcreateTable(){//DDL数据定义语句Connectionconn=newConnectionUtil().openConnection();Stringsql="createtableCustomerTbl(idintprimarykeyauto_increment,namevarchar(20),emailvarcha...