三、JDBC——Statement
来源:互联网 发布:ins聊天软件下载 编辑:程序博客网 时间:2024/05/21 15:42
Statement
statement语句是SQL语句的描述,使用它可以操作各种SQL语句,包括DDL(数据定义语句,如创建表)、DML(CRUD)和DCL等。
1、在src中的com包下创建Test2.java
package com;import java.sql.SQLException;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Statement;public class Test2 { public static void main(String []args){// CreateTbale();// Insert();// Update();// Delete(); } static void Delete(){ Connection connection=DBUtil.open(); String sqlString="delete from User where id>0 &&id<9"; try { Statement statement=(Statement)connection.createStatement(); statement.executeUpdate(sqlString); } catch (Exception e) { // TODO: handle exception }finally{ DBUtil.close(connection); } } //更新 static void Update(){ Connection conn=DBUtil.open(); String sql="update User set name='big cxg' where id>3"; try { Statement statement=(Statement) conn.createStatement(); statement.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtil.close(conn); } } //增加数据 static void Insert(){ Connection conn=DBUtil.open(); String sql="insert into User(name,email)values('cxg','cxg@qq.com')"; try { Statement statement=(Statement) conn.createStatement(); statement.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtil.close(conn); } } //创建表 static void CreateTbale(){ Connection conn=DBUtil.open(); String sql="create table User(id int primary key auto_increment,name varchar(20),email varchar(20))"; try { Statement statement=(Statement) conn.createStatement(); statement.execute(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ DBUtil.close(conn); } }}
2、DBUtil.java作为连接数据库
package com;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.Reader;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;import com.mysql.jdbc.Connection;public class DBUtil { //定义成不可修改的 private static String driver; private static String url; private static String username; private static String password; //把值都按要求填入 static { //获取配置文件的内容 Properties p=new Properties(); Reader reader; try { reader = new FileReader("src//config.properties");//配置文件路径 p.load(reader);//加载驱动文件 } catch (Exception e) { e.printStackTrace(); } driver=p.getProperty("driver"); url=p.getProperty("url"); username=p.getProperty("username"); password=p.getProperty("password"); } //打开数据库 public static Connection open() { try { //注册加载驱动 Class.forName(driver); //链接数据库 return (Connection) DriverManager.getConnection(url,username,password); } catch (Exception e) { e.printStackTrace(); } return null; } //关闭数据库 public static void close(Connection conn) { if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }}
阅读全文
0 0
- 三、JDBC——Statement
- JDBC—Connection、Statement、ResultSet
- JDBC(2)—Statement
- JDBC——Connection,Statement,PreparedStatement,CallableStatement
- JDBC——Connection,Statement,PreparedStatement,CallableStatement
- JDBC的使用——Statement
- jdbc statement 三种方式的比较
- jsp基础——JDBC(2)@Statement
- JDBC学习笔记(3)—Statement执行SQL语句
- jdbc statement
- JDBC-Statement
- JDBC Statement
- jdbc statement
- 【Jmeter】——Update Statement & Callable Statement & Select Statement 分析
- JavaWeb基础(2)—— JDBC 中 Statement 与 PrepareStatement 的区别
- JDBC 之 Statement 对象
- JDBC 之 Statement 对象
- JDBC (二) -- Statement对象
- 图像过滤(PAT-1066)
- java实现洗牌 发牌
- Solr IK分词 停止词 扩展词 创建core
- php操作redis中的hash类型数据的方法和代码例子
- Windows命令行查看文件的MD5
- 三、JDBC——Statement
- 机器学习第六周(三)
- 用程序实现更新PO的价格
- ios控件——UITarBarController的简单使用
- caffe 图片格式
- linux 管道命令
- [Leetcode-94中序-144前序-145后序]【Stack】递归和非递归方式遍历二叉树
- ios ffmpeg h264 encode
- jsp生成校验码