码迷,mamicode.com
首页 > 其他好文 > 详细

dljd_017_使用PreparedStatement执行模糊查询

时间:2018-12-06 10:16:22      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:span   stack   on()   pre   use   crud   .com   into   分享图片   

一、jdbc模糊查询(查询名字中带a的用户)

package edu.aeon.jdbc.crud;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.aeon.aeonutils.AeonJdbcUtils;
/**
 * [说明]:使用jdbc进行数据操作
 * @author aeon
 */
public class JDBC_insert {
    /**
     * 使用jdbc进行增加操作(DML)
     */
    public static void jdbc_insert(){
        Connection connection=null;
        PreparedStatement preparedStatement = null;
        try {
            connection = AeonJdbcUtils.getMySqlConnection();
            String sql="insert into user(userid,username,userpw) values (?,?,?)";
            //将sql语句进行预编译然后保存到preparedStatement对象中
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, 10006);
            preparedStatement.setString(2, "aeon");
            preparedStatement.setString(3, "aeon");
            int rowCount = preparedStatement.executeUpdate();
            System.out.println(rowCount+"条数据被插入!");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            AeonJdbcUtils.closeDB(null, preparedStatement, connection);
        }
    }
    /**
     * 查询操作(DQL)
     * @param args
     */
    public static void jdbc_select(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet  resultSet = null;
        try {
            connection=AeonJdbcUtils.getMySqlConnection();
            String sql="select userid,username,userpw from user where userid > ?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1, 10002);
            resultSet = preparedStatement.executeQuery();
            System.out.println("用户id\t用户名\t用户密码");
            while(resultSet.next()){
            int userid=resultSet.getInt("userid");
            String username=resultSet.getString("username");
            String userpw=resultSet.getString("userpw");
            System.out.println(userid+"\t"+username+"\t"+userpw);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            AeonJdbcUtils.closeDB(resultSet, preparedStatement, connection);
        }
    }
    
    /**
     * 模糊查询 查询名字中带a的用户
     * %a%:名字中包含a的
     * a%:以a开头的
     * _a%:名字第二个有a的
     * @param args
     */
    public static void jdbc_selectLike(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet  resultSet = null;
        try {
            connection=AeonJdbcUtils.getMySqlConnection();
            String sql="select userid,username,userpw from user where username like ?";
            preparedStatement=connection.prepareStatement(sql);
            //查询名字中带a的用户
            preparedStatement.setString(1, "%a%");
            resultSet = preparedStatement.executeQuery();
            System.out.println("用户id\t用户名\t用户密码");
            while(resultSet.next()){
            int userid=resultSet.getInt("userid");
            String username=resultSet.getString("username");
            String userpw=resultSet.getString("userpw");
            System.out.println(userid+"\t"+username+"\t"+userpw);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            AeonJdbcUtils.closeDB(resultSet, preparedStatement, connection);
        }
    }
    public static void main(String[] args) {
        //jdbc_insert();
        //jdbc_select();
        jdbc_selectLike();
    }
}

数据库截图:

技术分享图片

执行结果截图:

  技术分享图片

 

dljd_017_使用PreparedStatement执行模糊查询

标签:span   stack   on()   pre   use   crud   .com   into   分享图片   

原文地址:https://www.cnblogs.com/aeon/p/10074724.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!