JDBC_PrepareUpdate

2019-10-03  本文已影响0人  勇者与王者

package Java_JDBC;

import java.sql.*;

/**
 * @Author quzheng
 * @Date 2019/10/3 0:05
 * @Version 1.0
 */
public class JDBC_PrepareUpdate {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://192.168.8.14:3306/day22_JDBC";
        String user = "root";
        String password = "123456";
        Connection con = DriverManager.getConnection(url,user,password);

        String sql = "UPDATE sort set sname=?,sprice=? where sid=?";
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setObject(1,"汽车美容");
        pst.setObject(2,49988);
        pst.setObject(3,7);

        int row = pst.executeUpdate();

        pst.close();


        String search = "select * from sort";
        PreparedStatement pstSearch = con.prepareStatement(search);
        ResultSet rs = pstSearch.executeQuery();

        while (rs.next()){
            System.out.println(rs.getString("sid")+
                    "   "+rs.getString("sname")+
                    "   "+rs.getString("sprice")+
                    "   "+rs.getString("sdesc"));
        }


        rs.close();
        pstSearch.close();
        con.close();

    }
}


上一篇下一篇

猜你喜欢

热点阅读