发表于: 2018-09-17 20:08:33
1 376
今天完成了远程服务器运行jar包
public class TestDaoImpl implements TestDao {
public int getCountNum() {
System.out.println("start getCountNum method");
String sql = "SELECT count(*) FROM xzyuan";
int count = jdbcTemplate.queryForObject(sql, Integer.class); // 参数:SQL语句+返回类型的class
System.out.println("method result: " + count);
return count;
}
private DriverManagerDataSource dataSource;
private JdbcTemplate jdbcTemplate;
public TestDaoImpl() {
initDatabase();
}
private void initDatabase() {
// 设置数据库信息
dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/bm");
dataSource.setUsername("root");
dataSource.setPassword("123");
// 创建JdbcTemplate对象,设置数据源
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public int add(Test1 test) {
System.out.println("start add method");
String sql = "insert into xzyuan(name,qq,school) values(?,?,?)";
int rows = jdbcTemplate.update(sql, test.getName(), test.getQq(), test.getSchool());
System.out.println("method result: " + rows);
return rows;
}
@Override
public Test1 get(int id) {
String sql = "SELECT * FROM xzyuan WHERE id = ?";
Test1 test = jdbcTemplate.queryForObject(sql, new Test1RowMapper(), id);
return test;
}
@Override
public List<Test1> getAll() {
String sql = "SELECT * FROM xzyuan";
return jdbcTemplate.query(sql, new Test1RowMapper());
}
@Test
public void runTest1GetAll() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
TestDaoImpl testDaoImpl = (TestDaoImpl) applicationContext.getBean("testDaoImpl");
List<Test1> testList = testDaoImpl.getAll();
System.out.println(testList);
}
}
原因是
private void initDatabase() {
// 设置数据库信息
dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/bm");
dataSource.setUsername("root");
dataSource.setPassword("123");
之前url没有改过来
改完成后
远程数据库连接成功
成功插入语句
随后开始插入1000条循环调用
在这个环节
我发现之前可能复制粘贴的太多了
需要巩固
今天请教了师兄
搞了一下for循环
这也太给力了吧
起始时间在循环前面
结束时间在循环之后
}
long EndTime = System.currentTimeMillis();
System.out.println("用for循环arrayList 10000次花费时间:" + (EndTime - StartTime) + "毫秒");
}
}
然后断开db,从计算机管理里面关闭了MySQL服务
在来测试
添加了try catch
public class App {
public static void main(String[] args) {
long StartTime = System.currentTimeMillis();
try {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
TestDaoImpl testDaoImpl = (TestDaoImpl) applicationContext.getBean("testDaoImpl");
for (int i = 0; i < 1000; i++) {
Test1 test = new Test1();
test.setName("spring-jdbc");
test.setSchool("spring-password");
test.setQq("453456465");
testDaoImpl.add(test);
}
}
catch (Exception e) {
e.printStackTrace();
System.out.println("添加数据失败");
}
long EndTime = System.currentTimeMillis();
System.out.println("用for循环test 1000次花费时间:" + (EndTime - StartTime) + "毫秒");
}
}
运行之后
如果db有改动
改动的主要内容与数据库相关
改变实体类的数据
映射文件xml中定义的sql语句和映射规则
以及返回的结果集等等
插入1百万条数据
我直接插入可能要等很久
我决定去看看多线程如何操作
明天完成数据插入
评论