2018-05-22 JDBC之PrepareStatement

public class JDBCdemo {
    public static void main(String args[]) throws ClassNotFoundException,SQLException{
         Class.forName("com.mysql.jdbc.Driver");
         String url="jdbc:mysql://localhost:3306/mybase";
         String users="root";
         String password="123456";
         Connection conn=DriverManager.getConnection(url, users, password);
         String sql="UPDATE sort SET sname=?,sprice=? WHERE sid=?";
         PreparedStatement pst=conn.prepareStatement(sql);
         pst.setObject(1, "数码产品");
         pst.setObject(2, 409993);
         pst.setObject(3, 7);
         pst.executeUpdate();
         pst.close();
         conn.close();
         
    }
    
}

查询

public class JDBCdemo {
    public static void main(String args[]) throws ClassNotFoundException,SQLException{
         Class.forName("com.mysql.jdbc.Driver");
         String url="jdbc:mysql://localhost:3306/mybase";
         String users="root";
         String password="123456";
         Connection conn=DriverManager.getConnection(url, users, password);
         String sql="SELECT *FROM sort ";
         PreparedStatement pst=conn.prepareStatement(sql);
         ResultSet rs=pst.executeQuery();
         while(rs.next()) {
             System.out.println(rs.getString("sid")+"   "+rs.getString("sname")+"  "+rs.getString("sprice")+" "+rs.getString("sdescr")   );
         }
                  rs.close();
         pst.close();
         conn.close();
    
    }
    }

你可能感兴趣的:(2018-05-22 JDBC之PrepareStatement)