import pojo.Brand; import java.sql.*; public class Select { /** * * 添加:添加品牌 * * 查询:查询所有数据 * * 修改:根据id修改 * * 删除:根据id删除 */ public static void main(String[] args) { try { add(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } private static void add() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/vip21?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"; String user = "root"; String passwd = "123456"; String brandName = "三只"; Connection connection = DriverManager.getConnection(url, user, passwd); String selectSql = "SELECT * FROM tb_brand WHERE brand_name LIKE ?"; PreparedStatement preparedStatement = connection.prepareStatement(selectSql); preparedStatement.setString(1, "%" + brandName + "%"); ResultSet resultSet = preparedStatement.executeQuery(); while ( resultSet.next() ) { Brand brand = new Brand(); brand.setId(resultSet.getInt("id")); brand.setBrandName(resultSet.getString("brand_name")); brand.setCompanyName(resultSet.getString("company_name")); brand.setOrdered(resultSet.getInt("ordered")); brand.setDescription(resultSet.getString("description")); brand.setStatus(resultSet.getInt("status")); System.out.println( brand ); } preparedStatement.close(); connection.close(); } }