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

上传人:小飞机 文档编号:1877205 上传时间:2022-12-23 格式:DOCX 页数:12 大小:893.01KB
返回 下载 相关 举报
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement.docx_第1页
第1页 / 共12页
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement.docx_第2页
第2页 / 共12页
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement.docx_第3页
第3页 / 共12页
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement.docx_第4页
第4页 / 共12页
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement.docx_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement.docx》由会员分享,可在线阅读,更多相关《Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03JDBC Statement.docx(12页珍藏版)》请在三一办公上搜索。

1、Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之03.JDBC Statement(1)上季我们建立了与数据库的连接,本季我们将对数据库进行操作使用Statement 执行DDL、使用Statement 执行DML,常用的插入、更新、删除及查询数据哈 Statement 简介Statement 提供了一个操作数据库语句的功能,可通过它来创建表、插入记录、修改记录、删除记录等操作 获得Statement可以从数据库连接Connection中获得StatementConnection conn = new ConnectionUtil().getConnection()

2、;Statement stmt = conn.createStatement();ConnectionUtil.javapackage com.michael.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.util.Properties;public class ConnectionUtil /第一种方法public Connection getConnection()Connection conn = null;try /Class.forName加载驱动Class.forName(com.

3、mysql.jdbc.Driver);/DriverManager获得连接conn = DriverManager.getConnection(jdbc:mysql:/localhost:3306/jdbc_db,root,mysqladmin);return conn; catch (Exception e) e.printStackTrace();return null;/第二种方法public Connection getConnection(String driver,String url,String user,String password)Connection conn = nu

4、ll;try /Class.forName加载驱动Class.forName(driver);/DriverManager获得连接conn = DriverManager.getConnection(url,user,password);return conn; catch (Exception e) e.printStackTrace();return null;/第三种方法public Connection openConnection()String driver = ;String url = ;String user = ;String password = ;Properties

5、prop = new Properties();Connection conn = 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.forN

6、ame(driver);/DriverManager获得连接conn = DriverManager.getConnection(url,user,password);return conn; catch (Exception e) e.printStackTrace();return null; TestStatement.javapackage com.michael.jdbc;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;public class TestStatemen

7、t 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 blocke.printStackTrace(); Main.javapackage com.michael.main;import com.michael.jdbc.Conn

8、ectionUtil;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:/loc

9、alhost:3306/jdbc_db,root,mysqladmin);/第三种方法System.out.println(第三种方法:+cu.openConnection();TestStatement.getStatement(); 测试结果: 使用Statement 执行DDL可以使用Statement来执行一个数据定义语句,例如:创建一张表TestStatement.javapackage com.michael.jdbc;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;

10、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 blocke.printStackTrace();public static void createTable()/DDL数据

11、定义语句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 blocke.prin

12、tStackTrace();finallyif(conn!=null)try conn.close(); catch (SQLException e) conn = null;e.printStackTrace(); Main.javapackage com.michael.main;import com.michael.jdbc.ConnectionUtil;import com.michael.jdbc.TestStatement;public class Main /* * param args */public static void main(String args) Connect

13、ionUtil 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();TestStateme

14、nt.createTable(); 测试结果: 使用Statement 执行DML更新 Insert TestStatement.javapackage 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 Stat

15、ement stmt = conn.createStatement();System.out.println(stmt); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();public static void createTable()/DDL数据定义语句Connection conn = new ConnectionUtil().openConnection();String sql = create table CustomerTbl(id int primary key auto_in

16、crement,name varchar(20),email varchar(20);try Statement stmt = conn.createStatement();/执行SQL语句stmt.execute(sql); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallyif(conn!=null)try conn.close(); catch (SQLException e) conn = null;e.printStackTrace();/DML数据操作语句-CRUD:

17、create、retrive、update、deletepublic static void testInsert()/DDL数据定义语句Connection conn = new ConnectionUtil().openConnection();String sql = insert into CustomerTbl(name,email) values(Michael,michael);try Statement stmt = conn.createStatement();/执行SQL语句stmt.executeUpdate(sql); catch (SQLException e) /

18、TODO Auto-generated catch blocke.printStackTrace();finallyif(conn!=null)try conn.close(); catch (SQLException e) conn = null;e.printStackTrace(); Main.javapackage com.michael.main;import com.michael.jdbc.ConnectionUtil;import com.michael.jdbc.TestStatement;public class Main /* * param args */public

19、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(); 测试结果:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号