executeQuery()没有参数
现在可以用Navicat Premium 15
如上图所示
dao中是对基础数据的简单控制,一般来说就是crud四大类的数据库操作。
service中是通过调用dao中crud并加上相同复杂的逻辑之后,整合成的具有实际意义的业务操作。
如果你非要和mvc模型一一对应的话,dao层和service层都属于模型层(model)。
像我们这样处于语法学习阶段的新手,实现是一位的,多动手、多模仿,慢慢会形成自己的理解。mvc都是前辈们的编码经验,既然经验就不要语法那样非对即错,只要你的整个工程代码看起来合理有序即可。
以上所言都是自己的理解,希望对你有所帮助。谢谢你的私信。
他也是只在这个con取作用
问题解决了,我是用mysql可视化软件创建的数据库,创建数据表的时候数据库引擎默认用的是MyISAM不支持事务,要改成InnoDB才行,大家别和我犯一样的的错误
public Goddess get(Integer id) {
Goddess goddess = new Goddess();
try {
pmpt = conn.prepareStatement("select username,sex,age from imooc_goddess where id = ?");
pmpt.setInt(1,id);
rs = pmpt.executeQuery();
while(rs.next()){
goddess.setUsername(rs.getString("username"));
goddess.setSex(rs.getString("sex"));
goddess.setAge(rs.getInt("age"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return goddess;
}
报错
1111
创建表的时候在create_at 后面添加默认值NOW(),而且类型要为datetime才会同时显示日期和时间
比如:create_at DATATIME NOT NULL DEFAULT NOW()
如果不回滚,那么这个conn会记住出现异常之前执行的语句,在下次使用这个conn提交的时候,把这些记住的刷新到数据库
你得贴出来代码别人才知道怎么回事吧?
两个都是有事务的,添加交易信息那里
你把 create_at的数据类型换成timestamp 就可以了
你应该把鼠标放在上面。看下是什么错误
package com.xiong.lq.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.xiong.lq.db.C3P0Util; import com.xiong.lq.db.DBCPUtil; import com.xiong.lq.db.DB_util; import com.xiong.lq.model.Account; public class AccountDAO { public void insert(Account account) throws SQLException{ Connection conn =DB_util.getConnection(); PreparedStatement st =conn.prepareStatement("insert into account_info(account,amount) values (?,?)"); st.setString(1, account.getAccount()); st.setDouble(2, account.getAmount()); st.execute(); } public void update(Account account) throws SQLException{ Connection conn =DB_util.getConnection(); PreparedStatement st =conn.prepareStatement("update account_info set account =?,amount=? where id =?"); st.setString(1, account.getAccount()); st.setDouble(2, account.getAmount()); st.setInt(3, account.getId()); st.execute(); } public void delete(Account account) throws SQLException{ Connection conn =DB_util.getConnection(); PreparedStatement st =conn.prepareStatement("delete from account_info where id =?"); st.setInt(1, account.getId()); st.execute(); } public List<Account> query(Account account) throws SQLException{ List<Account> list =new ArrayList<Account>(); Connection conn =DB_util.getConnection(); StringBuilder sb =new StringBuilder(); sb.append("select * from account_info"); sb.append("where account like ?"); PreparedStatement st =conn.prepareStatement(sb.toString()); st.setString(1, "%"+account.getAccount()+"%"); ResultSet rs =st.executeQuery(); Account a =null; while(rs.next()){ a=new Account(); a.setAccount(rs.getString("account")); a.setAmount(rs.getDouble("amount")); a.setCreate_at(rs.getDate("create_at")); a.setId(rs.getInt("id")); list.add(a); } return list; } public Account query(int id) throws SQLException{ List<Account> list =new ArrayList<Account>(); Connection conn =DB_util.getConnection(); StringBuilder sb =new StringBuilder(); sb.append("select * from account_info"); sb.append(" where id like ?"); PreparedStatement st =conn.prepareStatement(sb.toString()); st.setInt(1, id); ResultSet rs =st.executeQuery(); Account a =null; while(rs.next()){ a=new Account(); a.setAccount(rs.getString("account")); a.setAmount(rs.getDouble("amount")); a.setCreate_at(rs.getDate("create_at")); a.setId(rs.getInt("id")); // list.add(a); } return a; } public Account queryByDbcp(int id) throws SQLException{ DBCPUtil db =new DBCPUtil(); List<Account> list =new ArrayList<Account>(); Connection conn =db.getConn(); StringBuilder sb =new StringBuilder(); sb.append("select * from account_info"); sb.append(" where id like ?"); PreparedStatement st =conn.prepareStatement(sb.toString()); st.setInt(1, id); ResultSet rs =st.executeQuery(); Account a =null; while(rs.next()){ a=new Account(); a.setAccount(rs.getString("account")); a.setAmount(rs.getDouble("amount")); a.setCreate_at(rs.getDate("create_at")); a.setId(rs.getInt("id")); // list.add(a); } return a; } public Account queryByC3P0(int id) throws SQLException{ C3P0Util c3p0 =new C3P0Util(); List<Account> list =new ArrayList<Account>(); Connection conn =c3p0.getConnection(); StringBuilder sb =new StringBuilder(); sb.append("select * from account_info"); sb.append(" where id like ?"); PreparedStatement st =conn.prepareStatement(sb.toString()); st.setInt(1, id); ResultSet rs =st.executeQuery(); Account a =null; while(rs.next()){ a=new Account(); a.setAccount(rs.getString("account")); a.setAmount(rs.getDouble("amount")); a.setCreate_at(rs.getDate("create_at")); a.setId(rs.getInt("id")); // list.add(a); } return a; } } package com.xiong.lq.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.xiong.lq.db.DB_util; import com.xiong.lq.model.TransInfo; public class TransInfoDAO { public void insert(TransInfo transInfo) throws SQLException{ Connection conn =DB_util.getConnection(); PreparedStatement st =conn.prepareStatement("insert into trans_info(source_id,source_account,destination_id,destination_account,amount) values(?,?,?,?,?)"); st.setInt(1, transInfo.getSource_id()); st.setString(2, transInfo.getSource_account()); st.setInt(3, transInfo.getDesitination_id()); st.setString(4, transInfo.getDesitination_account()); st.setDouble(5, transInfo.getAmount()); st.execute(); } public void update(TransInfo transInfo) throws SQLException{ Connection conn =DB_util.getConnection(); PreparedStatement st =conn.prepareStatement("update trans_info set source_id =?,source_account =?,destination_id =?,destination_account=?,amount=? where id =?"); st.setInt(1, transInfo.getSource_id()); st.setString(2, transInfo.getSource_account()); st.setInt(3, transInfo.getDesitination_id()); st.setString(4, transInfo.getDesitination_account()); st.setDouble(5, transInfo.getAmount()); st.setInt(6, transInfo.getId()); st.execute(); } public void delete(TransInfo transInfo) throws SQLException{ Connection conn =DB_util.getConnection(); PreparedStatement st =conn.prepareStatement("delete from trans_info where id=?"); st.setInt(1, transInfo.getId()); st.execute(); } public List<TransInfo> query(TransInfo transInfo) throws SQLException{ List<TransInfo> list =new ArrayList<TransInfo>(); Connection conn =DB_util.getConnection(); PreparedStatement st =conn.prepareStatement("select * from trans_info where id =?"); st.setInt(1, transInfo.getId()); ResultSet rs =st.getResultSet(); TransInfo t =null; while(rs.next()){ t.setId(rs.getInt("id")); t.setSource_id(rs.getInt("source_id")); t.setSource_account(rs.getString("source_account")); t.setDesitination_id(rs.getInt("destination_id")); t.setDesitination_account(rs.getString("desitination_account")); t.setAmount(rs.getDouble("amount")); list.add(t); } return list; } }
我见视频里也没有写GET的方法,别人的可以运行,我的就报错了
不管你直接采用JDBC进行数据库事务操作,还是使用基于Spring或EJB的声明式事务功能进行事务管理,它们最终都是使用底层数据库的事务管理功能 完成最终任务的。 控制事务的方法是使用底层数据库的事务管理方式
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP