增删改查——PreparedStatement接口

1、添加

package pers.Pre.add;

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

public class Add {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student mangement system";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
            String insertSql = "insert into score(studentno,courseno,usually,final)values(?,?,?,?)";
            ps = con.prepareStatement(insertSql);// 获取预处理对象
            ps.setString(1, "20191832");
            ps.setString(2, "b221");
            ps.setDouble(3, 123);
            ps.setDouble(4, 123);
            int num = ps.executeUpdate();
            System.out.println("添加了" + num + "条记录");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null)
                    ps.close();
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

2、删除

package pers.Pre.jdbc;

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

public class DeleteDemo {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student mangement system";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
            String deleteSql = "Delete from Student where sname=?";
            ps = con.prepareStatement(deleteSql);// 获取预处理对象
            ps.setString(1,"秀儿");
            int num = ps.executeUpdate();
            System.out.println("删除了" + num + "条信息");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null)
                    ps.close();
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

}

3、修改

package pers.Pre.jdbc;

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

public class UpdateDemo {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student mangement system";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
            String updateSql = "Update Login set password=? where account ='学前班秀儿'";
            ps = con.prepareStatement(updateSql);// 获取预处理对象
            ps.setString(1,"201908");
            int num = ps.executeUpdate();
            System.out.println("更改了" + num + "条信息");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null)
                    ps.close();
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

}

4、查询

package pers.Pre.jdbc;

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

public class SelectDemo {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student mangement system";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
            String selectSql = "select * from score";
            ps = con.prepareStatement(selectSql);// 获取预处理对象
            rs = ps.executeQuery(); 
            System.out.println("     "+"学号"+"                        "+"班级"+"           "+"平时成绩"+"    "+"期末成绩");
            while (rs.next()) {
            
                System.out.println( rs.getString(1)+"  "+rs.getString(2)+"  "+rs.getString(3)+"  "+
                        rs.getString(4));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null)
                    ps.close();
                if (con != null)
                    con.close();
                if (rs!=null)
                    rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

}

5、Statement接口与PreparedStatement接口的对比

Statement接口用于处理不带参数的静态sql语句,PreparedStatement接口可以处理带参数的sql语句。

 

转载于:https://www.cnblogs.com/zhai1997/p/11376248.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值