Select.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import pojo.Brand;
  2. import java.sql.*;
  3. public class Select {
  4. /**
  5. * * 添加:添加品牌
  6. * * 查询:查询所有数据
  7. * * 修改:根据id修改
  8. * * 删除:根据id删除
  9. */
  10. public static void main(String[] args) {
  11. try {
  12. add();
  13. } catch (ClassNotFoundException e) {
  14. e.printStackTrace();
  15. } catch (SQLException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. private static void add() throws ClassNotFoundException, SQLException {
  20. Class.forName("com.mysql.jdbc.Driver");
  21. String url = "jdbc:mysql://localhost:3306/vip21?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
  22. String user = "root";
  23. String passwd = "123456";
  24. String brandName = "三只";
  25. Connection connection = DriverManager.getConnection(url, user, passwd);
  26. String selectSql = "SELECT * FROM tb_brand WHERE brand_name LIKE ?";
  27. PreparedStatement preparedStatement = connection.prepareStatement(selectSql);
  28. preparedStatement.setString(1, "%" + brandName + "%");
  29. ResultSet resultSet = preparedStatement.executeQuery();
  30. while ( resultSet.next() ) {
  31. Brand brand = new Brand();
  32. brand.setId(resultSet.getInt("id"));
  33. brand.setBrandName(resultSet.getString("brand_name"));
  34. brand.setCompanyName(resultSet.getString("company_name"));
  35. brand.setOrdered(resultSet.getInt("ordered"));
  36. brand.setDescription(resultSet.getString("description"));
  37. brand.setStatus(resultSet.getInt("status"));
  38. System.out.println( brand );
  39. }
  40. preparedStatement.close();
  41. connection.close();
  42. }
  43. }