zoukankan      html  css  js  c++  java
  • Java数据库——PreparedStatement接口

    PreparedStatement接口是Statement的子接口,属于预处理操作,与直接使用Statement不同的是,PreparedStatement在操作时,是先在数据表中准备好了一条SQL语句,但是此SQL语句的具体内容暂时不设置,而是之后再进行设置。

    使用PreparedStatement完成数据的增加和查询操作

    //=================================================
    // File Name       :	PreparedStatement_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    import java.sql.*;
    import java.text.SimpleDateFormat;
    
    //主类
    //Function        : 	PreparedStatement_demo
    public class PreparedStatement_demo {
    
    	//定义MySQL的数据库驱动程序
    	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    	//定义MySQL数据库的连接地址
    	public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
    	//MySQL数据库的连接用户名
    	public static final String DBUSER = "root";
    	//MySQL数据库的连接密码
    	public static final String DBPASS = "123456";
    	
    	public static void main(String[] args) throws Exception{
    		// TODO 自动生成的方法存根
    		Connection conn = null;						//数据库连接
    		PreparedStatement pstmt = null;	//数据库操作
    		
    		String name = "王五";
    		String password = "pwd2";
    		int age = 25;
    		String sex = "女";
    		String birthday = "2002-11-21";
    		java.util.Date temp = null;				//声明一个Date对象
    		//通过SimpleDateFormat类将一个字符串变成java.util.Date类型
    		temp = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
    		//通过java.util.Date取出具体的日期数,并将其变成java.sql.Date类型
    		java.sql.Date bir = new java.sql.Date(temp.getTime());
    		String sql = "INSERT INTO user(name,password,age,sex,birthday)"+"VALUES(?,?,?,?,?)";//编写预处理SQL
    		Class.forName(DBDRIVER);			//加载驱动程序
    		//连接MySQL数据库时,要写上连接的用户名和密码
    		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    		pstmt = conn.prepareStatement(sql);		//实例化PreparedStatement
    		pstmt.setString(1, name);					//设置第一个“?”的内容
    		pstmt.setString(2, password);			//设置第二个“?”的内容
    		pstmt.setInt(3, age);								//设置第三个“?”的内容
    		pstmt.setString(4, sex);							//设置第四个“?”的内容
    		pstmt.setDate(5, bir);							//设置第五个“?”的内容
    		pstmt.executeUpdate();						//执行数据库更新操作,不需要SQL
    		
    		pstmt.close(); 										//操作关闭
    		conn.close(); 										//数据库关闭
    		
    	}
    
    }
    

    模糊查询

    //=================================================
    // File Name       :	PreparedStatement_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    import java.sql.*;
    import java.text.SimpleDateFormat;
    
    //主类
    //Function        : 	PreparedStatement_demo
    public class PreparedStatement_demo {
    
    	//定义MySQL的数据库驱动程序
    	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    	//定义MySQL数据库的连接地址
    	public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";
    	//MySQL数据库的连接用户名
    	public static final String DBUSER = "root";
    	//MySQL数据库的连接密码
    	public static final String DBPASS = "123456";
    	
    	public static void main(String[] args) throws Exception{
    		// TODO 自动生成的方法存根
    		
    		Connection conn = null;						//数据库连接
    		PreparedStatement pstmt = null;	//数据库操作
    		String keyWord = "王";							//设置查询关键字
    		String keyWord1 = "男";							//设置查询关键字
    		ResultSet rs = null;								//保存查询结果
    		String sql = "SELECT id,name,password,age,sex,birthday"+
    				" FROM user WHERE name LIKE ? OR password LIKE ? OR sex LIKE ?";
    		Class.forName(DBDRIVER);			//加载驱动程序
    		//连接MySQL数据库时,要写上连接的用户名和密码
    		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    		pstmt = conn.prepareStatement(sql);		//实例化PreparedStatement
    		pstmt.setString(1, "%"+keyWord+"%");					//设置第一个“?”的内容
    		pstmt.setString(2, "%"+keyWord+"%");					//设置第一个“?”的内容
    		pstmt.setString(3, "%"+keyWord1+"%");					//设置第一个“?”的内容
    		rs = pstmt.executeQuery();						//实例化ResultSet对象
    		while(rs.next()){
    			int id = rs.getInt(1);
    			String name = rs.getString(2);
    			String pass = rs.getString(3);
    			int age = rs.getInt(4);
    			String sex = rs.getString(5);
    			java.util.Date d = rs.getDate(6);
    			System.out.println("编号:"+id);
    			System.out.println("名字:"+name);
    			System.out.println("密码:"+pass);
    			System.out.println("年龄:"+age);
    			System.out.println("性别:"+sex);
    			System.out.println("生日:"+d);
    		}
    		rs.close();												//关闭结果集
    		pstmt.close(); 										//操作关闭
    		conn.close(); 										//数据库关闭
    		
    	}
    
    }
    

     

  • 相关阅读:
    数据类型及转换
    进制转换
    精通libGDX-RPG开发实战
    github上最好的开源MMORPG
    同步mysql数据到ElasticSearch的最佳实践
    在libGDX中使用Spine骨骼动画
    window下Kafka最佳实践
    linux 系统的负载与CPU、内存、硬盘、用户数监控脚本[marked]
    源码安装cmake(或者叫升级cmake)
    Rust-HayStack
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5303373.html
Copyright © 2011-2022 走看看