JDBC之preparedStatement

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

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');
全部评论

相关推荐

头像
02-21 16:31
长沙理工大学
大家好,今天分享一个很贴合目前校招时间段的提问:Up你好,本人双非本科大四,软件工程专业。大学前两年因为感觉前端好学,岗位也多选择学习前端。但那时比较懒散,课也多,所以前端也没有学多好。后来互联网寒冬,觉得出去不好找工作。就在大三下开始准备考研,但在去年10月份放弃考研(因为家里的一些事故,一个半月没有复习考研),处理好后,剩70多天感觉考不上值得上的学校。所以干脆准备就业,但感觉前端这个方向特别凉,于是换成了Linux c++方向(为此拒绝了一个前端实习)10月底到现在复习了c语言,学习了C++语法,特性,包括STL这些。学习了Linux系统编程进程线程,网络编程tcp/udp,多路转接,l...
牛客230000345号:毕业入坑两年,提点参考的东西吧,建议边找边备研,学历才是第一生产力,后期如果你要职业发展,这是最基本的几个了,工作和晋升除了项目经验,不就是比的派个人学历、吹牛能力和一堆头衔了(晋升的话,派系很重要)。 工作方面,不了解服务端,但是你可以看招聘,其实相比来说qt在客户端和服务端都可以用到,而且跨平台兼容性好,而且qt不就是c+++吗(学好c++,用哪个框架都不头痛),qt不只是给你个UI界面,封装的很多东西都是可以借鉴的。看你想去哪个城市,现在长沙软件行情不好,真心建议没上岸可以去深圳看看,长沙这边工资对标深圳砍半(眼泪流下来),长沙不少大一点私企面试的也开始卷学历卷项目(双非泪奔),如果想去国企你要能吹当然也可以(其实国企也就那12%的公积金了,并不稳定,但是稳定穷是肯定的)。 想去好一点的,建议把基础打牢,学历一定要提高(长期发展一定要,国内还是不少地方学历论的),如果有实习期建议能参与公司项目就参与,不然只会被拷打,最好从项目或者demo里把设计模式、指针、特性、模板、多线程实现并发并行、通讯协议、数据库这些基本的学会一部分,建议再学学qml和Linux,最好学一点嵌入式(Linux用在嵌入式板挺多的),掌握一门脚本语言(Python,Python,Python)和git或者svn代码管理,没签合同(不是三方),你还是校招生,校招只有一次(当然也可以说是本科一次,硕士一次,博士一次),用了错过就没有了,好多公司最喜欢招应届生了,一张白纸(又便宜又容易被PUA)。 最后,其实纠结这么多,不如第一份工作就选你最喜欢的编程语言、框架和操作系统,反正都是牛马,也不一定只吃一家喂的草
点赞 评论 收藏
分享
01-17 12:35
吉首大学 Java
秋招之BrianGriffin:自己的工作自己做!😡
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务