课时6:PrepareStatement语句

来源:互联网 发布:php项目进度管理系统 编辑:程序博客网 时间:2024/05/01 10:01
1.PreparedStatement为预定义语句,继承Statement。
2.Statement只能静态操作SQL语句,如果要想动态操作SQL语句又该如何实现呢?例如:注册会员,这里可以使用PreparedStatement来动态操作SQL语句,PreparedStatement通过使用占位符“?”,来预生成SQL语句,从而达到动态操作的功能
3.实例
3.1

面向对象,就是尽量把对象定义成一个java文件,所有属性和操作都定义到里面。在其他函数中直接创建对象和调用对象

Test4.java

package com.geek99.demo;

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

public class Test4 {

    public static void main(String[] args) {
    //    insert("tom1","tom1@111111.com");
    //    Customer c = new Customer();
    //    c.setName("rose");
    //    c.setEmail("rose@1111.com");
    //    insert(c);

//        c.setId(1);
//        c.setName("dsfasdfa");
//        update(c);

//        del(2);
        Customer c=query(3);
        if(c!=null)
            System.out.println(c.getName()+":"+c.getEmail());
    }

    static void insert(String name, String email) {
        Connection conn = DBUtil.open();
        String sql = "insert into CustomerTbl(name,email)values(?,?)";

        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, email);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    static void insert(Customer c) {
        Connection conn = DBUtil.open();
        String sql = "insert into CustomerTbl(name,email)values(?,?)";

        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, c.getName());
            pstmt.setString(2, c.getEmail());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    static void update(Customer c) {
        Connection conn = DBUtil.open();
        String sql = "update CustomerTbl set name=? where id=?";

        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, c.getName());
            pstmt.setInt(2, c.getId());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    static void del(int id) {
        Connection conn = DBUtil.open();
        String sql = "delete from CustomerTbl where id=?";

        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);        
            pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    static Customer query(int id) {
        Connection conn = DBUtil.open();
        String sql = "select id,name,email from CustomerTbl where id=?";

        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            ResultSet rs=pstmt.executeQuery();
        //    pstmt.executeUpdate();

            if(rs.next()){
                String name=rs.getString(2);
                String email=rs.getString(3);
                Customer c=new Customer();
                c.setId(id);
                c.setName(name);
                c.setEmail(email);
                return c;
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


}

0 0
原创粉丝点击