DruidTest.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package pojo;
  2. import com.alibaba.druid.pool.DruidDataSourceFactory;
  3. import javax.sql.DataSource;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.util.Properties;
  10. public class DruidTest {
  11. public static void main(String[] args) {
  12. try {
  13. test();
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. private static void test() throws Exception {
  21. Properties properties = new Properties();
  22. properties.load( new FileInputStream("day03/src/druid.properties"));
  23. DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
  24. //System.out.println( dataSource );
  25. String brandName = "香飘飘奶茶";
  26. String companyName = "奶茶";
  27. int ordered = 30;
  28. String description = "一年绕地球一周";
  29. int status = 2;
  30. String insertSql = "INSERT INTO tb_brand (brand_name, company_name, ordered, description, status) values (?,?,?,?,?)";
  31. Connection connection = dataSource.getConnection();
  32. PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
  33. preparedStatement.setString(1, brandName);
  34. preparedStatement.setString(2, companyName);
  35. preparedStatement.setInt(3, ordered);
  36. preparedStatement.setString(4, description);
  37. preparedStatement.setInt(5, status);
  38. int i = preparedStatement.executeUpdate();
  39. if ( i > 0 ) {
  40. System.out.println("插入成功");
  41. } else {
  42. System.out.println("插入失败");
  43. }
  44. preparedStatement.close();
  45. }
  46. }