1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package pojo;
- import com.alibaba.druid.pool.DruidDataSourceFactory;
- import javax.sql.DataSource;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.util.Properties;
- public class DruidTest {
- public static void main(String[] args) {
- try {
- test();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void test() throws Exception {
- Properties properties = new Properties();
- properties.load( new FileInputStream("day03/src/druid.properties"));
- DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
- //System.out.println( dataSource );
- String brandName = "香飘飘奶茶";
- String companyName = "奶茶";
- int ordered = 30;
- String description = "一年绕地球一周";
- int status = 2;
- String insertSql = "INSERT INTO tb_brand (brand_name, company_name, ordered, description, status) values (?,?,?,?,?)";
- Connection connection = dataSource.getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
- preparedStatement.setString(1, brandName);
- preparedStatement.setString(2, companyName);
- preparedStatement.setInt(3, ordered);
- preparedStatement.setString(4, description);
- preparedStatement.setInt(5, status);
- int i = preparedStatement.executeUpdate();
- if ( i > 0 ) {
- System.out.println("插入成功");
- } else {
- System.out.println("插入失败");
- }
- preparedStatement.close();
- }
- }
|