JDBC


import org.junit.Test;

import java.sql.*;

/**
 * Created by Administrator on 2017/7/24.
 */
public class A {
    // sql :insert del update

    @Test
    public void add(){
        Connection conn=null;
        PreparedStatement pstmt=null;

        /**
         * Sets the designated parameter to the given Java <code>int</code> value.
         * The driver converts this
         * to an SQL <code>INTEGER</code> value when it sends it to the database.
         *
         * @param parameterIndex the first parameter is 1, the second is 2, ...
         * @param x the parameter value
         * @exception SQLException if parameterIndex does not correspond to a parameter
         * marker in the SQL statement; if a database access error occurs or
         * this method is called on a closed <code>PreparedStatement</code>
         */
        try {
            Class.forName("com.mysql.jdbc.Driver");    // 1加载驱动进入内存
            //2连接数据库
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/bz?useUnicode=true&characterEncoding=utf-8","root","root");

            //3 执行sql语句 prepareStatement:可以有效防止二次执行同一sql,提供性能

            pstmt= conn.prepareStatement("insert into hus(id,hname) values(?,?)");
            pstmt.setInt(1,6);
            pstmt.setString(2,"JACK");
            //4遍历数据
            System.out.println(
            pstmt.executeUpdate()>0?"成功":"失败");



        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //5关闭
            if(pstmt!=null){
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }


        }

    }


//    @Test
//    public void del(){
//        Connection conn=null;
//        PreparedStatement pstmt=null;
//
//
//        try {
//            Class.forName("com.mysql.jdbc.Driver");    // 1加载驱动进入内存
//            //2连接数据库
//            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/bz?useUnicode=true&characterEncoding=utf-8","root","root");
//
//            //3 执行sql语句 prepareStatement:可以有效防止二次执行同一sql,提供性能
//
//            pstmt= conn.prepareStatement("DELETE from hus where id=?");
//            pstmt.setInt(1,2);
//
//            //4遍历数据
//            System.out.println(
//                    pstmt.executeUpdate()>0?"成功":"失败");
//
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally{
//            //5关闭
//            if(pstmt!=null){
//                try {
//                    pstmt.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//            if(conn!=null){
//                try {
//                    conn.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//
//
//        }
//
//    }
//    @Test
//    public void update(){
//        Connection conn=null;
//        PreparedStatement pstmt=null;
//
//
//        try {
//            Class.forName("com.mysql.jdbc.Driver");    // 1加载驱动进入内存
//            //2连接数据库
//            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/bz?useUnicode=true&characterEncoding=utf-8","root","root");
//
//            //3 执行sql语句 prepareStatement:可以有效防止二次执行同一sql,提供性能
//
//            pstmt= conn.prepareStatement("update  hus set hname=? where id=?");
//            pstmt.setString(1,"JSON");
//            pstmt.setInt(2,3);
//
//            //4遍历数据
//            System.out.println(
//                    pstmt.executeUpdate()>0?"成功":"失败");
//
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally{
//            //5关闭
//            if(pstmt!=null){
//                try {
//                    pstmt.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//            if(conn!=null){
//                try {
//                    conn.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//
//
//        }
//
//    }
//
//    @Test
//    public void query(){
//        Connection conn=null;
//        PreparedStatement pstmt=null;
//        ResultSet rs=null;
//
//        try {
//            Class.forName("com.mysql.jdbc.Driver");    // 1加载驱动进入内存
//            //2连接数据库
//            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/bz?useUnicode=true&characterEncoding=utf-8","root","root");
//
//            //3 执行sql语句 prepareStatement:可以有效防止二次执行同一sql,提供性能
//            int onum=20005;//sql 注入攻击
//            pstmt=conn.prepareStatement("select * from orderitems where order_num=? and item_price<?");
//            pstmt.setInt(1,onum);
//            pstmt.setFloat(2,10);
//            //4遍历数据
//            rs=pstmt.executeQuery();
//
//            while(rs.next()){
//                System.out.printf("%.2f %d \n",rs.getFloat("item_price"),rs.getInt(1));
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally{
//            if(rs!=null){
//                try {
//                    rs.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }       //5关闭
//
//            if(pstmt!=null){
//                try {
//                    pstmt.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//            if(conn!=null){
//                try {
//                    conn.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//
//        }
//
//
//    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值