发表于: 2019-12-31 23:58:48
1 1070
今天做的事:
1.花了点时间看 java 基础语法,《疯狂 java 讲义》看到多维数组,正好我买的是第四版,主要讲 jdk8 jdk9,修真院刚好也让我们用的是 jdk8。
java 的话,还是要分清楚虚拟机里面的堆、栈和常量池,比如 String 就是个引用类型
String str = "祝大家新年快乐!";
这里,str 其实是个引用,放在栈里,初始化的字符串放在常量池里面,编译的时候发现其他一样的字符串也指向常量池的这个字符串以节省空间。同时也方便进行 == 比较。
对于引用类型来说 == 比较的是引用,也就是是否指向同一个地址,equals 比较的是对象内的字段。
而在 int,char 等等这些“值类型”里,== 比较的就是值。我觉得这也是 java 容易混淆的一个地方。
2.改了昨天的项目,增加了 dao 接口,给接口做了实现,然后写了测试类,通过了测试。
工具类:
package cn.mogeek.testJDBC;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtil {
/**
* 连接数据库
* @return 返回数据连接
* @throws Exception
*/
public static Connection getConnection() throws Exception {
InputStream inputstream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(inputstream);
String url = properties.getProperty("jdbc.url");
String user = properties.getProperty("jdbc.user");
String passwd = properties.getProperty("jdbc.passwd");
String driverClass = properties.getProperty("jdbc.driverClass");
Class.forName(driverClass);
Connection connection = DriverManager.getConnection(url, user, passwd);
return connection;
}
/**
* 释放数据库资源
* @param resultset
* @param statement
* @param connection
*/
public static void release(ResultSet resultset, Statement statement, Connection connection)
{
if (resultset != null) {
try {
resultset.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
数据库配置:
jdbc.url = jdbc:mysql:///school
jdbc.user = root
jdbc.passwd = mogeek666
jdbc.driverClass = com.mysql.jdbc.Driver
dao:
package cn.mogeek.dao;
import cn.mogeek.domain.Disciple;
import java.util.List;
/**
* DiscipleDao 访问接口
*/
public interface DiscipleDao {
/**
* 查询所有报名的弟子
* @return 返回一个弟子信息的list
*/
public List<Disciple> query();
/**
* 保存一个新弟子
* @param disciple 弟子对象
*/
public void save(Disciple disciple);
}
implements:
package cn.mogeek.dao;
import cn.mogeek.domain.Disciple;
import cn.mogeek.testJDBC.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class DiscipleDaoImpl implements DiscipleDao {
@Override
public List<Disciple> query() {
List<Disciple> disciples = new ArrayList<Disciple>();
Disciple disciple;
Connection connection = null;
//Statement sta = null;
ResultSet resultSet = null;
PreparedStatement psta = null;
String sql = "select * from jnshu_copy1";
try {
connection = JDBCUtil.getConnection();
psta = connection.prepareStatement(sql);
resultSet = psta.executeQuery();
while (resultSet.next()){
int id = resultSet.getInt("id");
int qq = resultSet.getInt("qq");
String name = resultSet.getString("name");
String object = resultSet.getString("object");
String graduated_school = resultSet.getString("graduated_school");
String brother = resultSet.getString("brother");
String from = resultSet.getString("comefrom");
String daily_report = resultSet.getString("daily_report");
String aims = resultSet.getString("aims");
disciple = new Disciple();
disciple.setAims(aims);
disciple.setId(id);
disciple.setBrother(brother);
disciple.setDaily_report(daily_report);
disciple.setFrom(from);
disciple.setName(name);
disciple.setObject(object);
disciple.setQq(qq);
disciples.add(disciple);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtil.release(resultSet, psta, connection);
}
return disciples;
}
@Override
public void save(Disciple disciple) {
Connection connection = null;
//Statement sta = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
String sql = "insert into jnshu_copy1(id, qq, name, object, graduated_school, brother, comefrom, daily_report, aims) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
try {
connection = JDBCUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, disciple.getId());
preparedStatement.setInt(2, disciple.getQq());
preparedStatement.setString(3, disciple.getName());
preparedStatement.setString(4, disciple.getObject());
preparedStatement.setString(5, disciple.getGraduated_school());
preparedStatement.setString(6, disciple.getBrother());
preparedStatement.setString(7, disciple.getFrom());
preparedStatement.setString(8, disciple.getDaily_report());
preparedStatement.setString(9, disciple.getAims());
preparedStatement.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtil.release(resultSet, preparedStatement, connection);
}
}
}
test:
package cn.mogeek.dao;
import cn.mogeek.domain.Disciple;
import org.junit.Test;
import java.util.List;
public class DiscipleDaoImplTest {
@Test
public void testDiscipleDaoImpl(){
DiscipleDao discipleDao = new DiscipleDaoImpl();
List<Disciple> disciples = discipleDao.query();
for (Disciple disciple : disciples) {
System.out.println("id: " + disciple.getId()
+ ", name: " + disciple.getName()
+ ", object: " + disciple.getObject()
+ ", aims: " + disciple.getAims()
+ ", qq: " + disciple.getQq()
+ ", brother: " + disciple.getBrother()
+ ", from: " + disciple.getFrom()
+ ", daily_report: " + disciple.getDaily_report()
+ ", graduated_school: " + disciple.getGraduated_school()
);
}
}
@Test
public void testSave(){
Disciple disciple = new Disciple();
disciple.setId(888);
disciple.setQq(666666);
disciple.setObject("后端工程师");
disciple.setName("暗灭");
disciple.setFrom("大明湖畔");
disciple.setDaily_report("https://www.zhihu.com/people/ptteng/activities");
disciple.setBrother("不知道");
disciple.setAims("帅就完事了");
disciple.setGraduated_school("郑州大学");
DiscipleDaoImpl discipleDao = new DiscipleDaoImpl();
discipleDao.save(disciple);
}
}
遇到的问题:
1.写测试的时候,测试类的类名别和被测试的类名一致,比如我这里要被测试的类 DiscipleDaoImpl,然后一开始我的测试类类名也是这个,一直报错找不到符号。
2.列名一定不要设置为关键词!
列名一定不要设置为关键词!
列名一定不要设置为关键词!
本来有个列名是 from 用命令行测试的时候我也踩过坑,然后到这里写的时候我记错了以为是加个单引号就可以了,一直调不通,试了 n 遍····
真没必要和这种事较劲,别设置成关键词就好了····
这一天,又在和自己较劲中度过。
一次编译,处处报错。
明天的计划:
1.用 JdbcTemplate 连接数据库。
评论