JDBC之preparedStatement

本文提供了一个Java应用程序示例,展示了如何使用PreparedStatement进行数据库操作,包括插入、删除、更新和查询等基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接贴代码案例,理论知识可以从隔壁看看。

prestmtest.java

package PREPAREDSTAMENT;

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

import org.junit.Test;

public class prestmtest {
          String url="jdbc:mysql://localhost:3306/data";
          String user="root";
          String password="123456";
          @Test

          /*
           * 增加
          */
          public  void testInsert(){
            Connection conn=null;
            PreparedStatement pstmt=null;
            //获取链接对象
           try {
            Class.forName("com.mysql.jdbc.Driver");
             conn=DriverManager.getConnection(url, user, password);
                //准备预编译sql语句
             String sql="insert into xxb values(?,?,?)";
             //执行sql
             pstmt=conn.prepareStatement(sql);
             //设置参数


            pstmt.setInt(1, 4);
             pstmt.setString(2, "杨六");
             pstmt.setString(3, "女");
             int count=pstmt.executeUpdate();
             System.out.print("影响了"+count+"行");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new RuntimeException(e);
        } //throw new RuntimeException(e);
           finally{
             if(pstmt!=null)
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
             if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
           }
          }
          /*
           * 删除
          */
    public  void testDelete(){
        Connection conn=null;
        PreparedStatement pstmt=null;
        //获取链接对象
     try {
        Class.forName("com.mysql.jdbc.Driver");
         conn=DriverManager.getConnection(url, user, password);
            //准备预编译sql语句
         String sql="delete from xxb where sid=?";
         //执行sql
         pstmt=conn.prepareStatement(sql);
         //设置参数
         pstmt.setInt(1, 4);
         int count=pstmt.executeUpdate();
         System.out.print("影响了"+count+"行");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new RuntimeException(e);
    } //throw new RuntimeException(e);
     finally{
         if(pstmt!=null)
            try {
                pstmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                throw new RuntimeException(e);
            }
         if(conn!=null)
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                throw new RuntimeException(e);
            }
     }
          }

          /*
           * 修改
          */
          public  void testUpdate(){
            Connection conn=null;
            PreparedStatement pstmt=null;
            //获取链接对象
           try {
            Class.forName("com.mysql.jdbc.Driver");
             conn=DriverManager.getConnection(url, user, password);
                //准备预编译sql语句
             String sql="update xxb set sname=? where sid=?";
             //执行sql
             pstmt=conn.prepareStatement(sql);
             //设置参数



             pstmt.setString(1, "张三");
             pstmt.setInt(2, 1);
             int count=pstmt.executeUpdate();
             System.out.print("影响了"+count+"行");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new RuntimeException(e);
        } //throw new RuntimeException(e);
           finally{
             if(pstmt!=null)
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
             if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
           }
                }  
          /*
           * 查询
         */
          public  void testQuery(){
            Connection conn=null;
            PreparedStatement pstmt=null;
            //获取链接对象
           try {
            Class.forName("com.mysql.jdbc.Driver");
             conn=DriverManager.getConnection(url, user, password);
                //准备预编译sql语句
             String sql="select *from xxb";
             //执行sql
             pstmt=conn.prepareStatement(sql);
             //遍历
             ResultSet rs=pstmt.executeQuery();
             while(rs.next()){
                 int id=rs.getInt("sid");
                 String name=rs.getString("sname");
                 String gender=rs.getString("sgender");
                 System.out.println(id+" "+name+" "+gender);
             }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new RuntimeException(e);
        } //throw new RuntimeException(e);
           finally{
             if(pstmt!=null)
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
             if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
           }
                }     
}

数据库文件


SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
--  Table structure for `xxb`
-- ----------------------------
DROP TABLE IF EXISTS `xxb`;
CREATE TABLE `xxb` (
  `sid` int(11) default NULL,
  `sname` varchar(20) default NULL,
  `sgender` varchar(20) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records 
-- ----------------------------
INSERT INTO `xxb` VALUES ('1','zhangsan','man'), ('2','lisi','woman'), ('3','wangwu','man');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值