报刊订阅管理系统
userName.addActionListener(this); login.addActionListener(this); register.addActionListener(this); exit.addActionListener(this); }
public static void main(String[] args) {
Main f=new Main();
f.setTitle(\报刊订阅管理系统\ }
public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根
if(e.getSource()==register) //注册(登录、退出类似) {
new UsersRegister();//过渡到新的窗口Menu; this.dispose();//释放当前窗口 } }
3.2数据录入实现
录入报刊信息(录入用户信息和对用户、报刊、订单的增删改查与之类似) (1)LoggingdataNewspaper.java
public static void main(String[] args) {
LoggingdataNewspaper f=new LoggingdataNewspaper(); f.setTitle(\报刊信息\ }
public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根
if(e.getSource()==button5) // 退出 {
new ManagerHome();//过渡到新的窗口Menu; this.dispose();//释放当前窗口 }
if(e.getSource()==button1) // 添加 {
//定义一个空的newspaper对象 newspaper news=new newspaper(); //将数据封装到news中
news.setNewsNo(text1.getText().trim());
9 / 32
报刊订阅管理系统
news.setNewsName(text2.getText().trim()); news.setPublish(text3.getText().trim()); news.setPubPeriod(text4.getText().trim()); news.setContent(text5.getText().trim());
news.setPrice(Float.parseFloat(text6.getText().trim())); System.out.println(news); //定义一个控制对象
InformationDaoImpl ifd=new InformationDaoImpl(); //执行添加用户操作 ifd.addNews(news); }
if(e.getSource()==button2) // 删除 {
//定义一个空的newspaper对象
newspaper delnews=new newspaper(); //将数据封装到delnews中
delnews.setNewsNo(text1.getText().trim()); delnews.setNewsName(text2.getText().trim());
System.out.println(delnews); //定义一个控制对象
InformationDaoImpl ifd=new InformationDaoImpl(); //执行添加用户操作
ifd.deleteNews(delnews); }
if(e.getSource()==button3) // 查询 {
//定义一个空的newspaper对象 newspaper n=new newspaper(); //将数据封装在n中
String news=text1.getText().trim(); //定义一个控制对象
InformationDaoImpl ifd=new InformationDaoImpl(); n=ifd.FindNewsByNewspaper(news); System.out.println(n);
text2.setText(n.getNewsName()); text3.setText(n.getPublish()); text4.setText(n.getPubPeriod()); text5.setText(n.getContent());
text6.setText(String.valueOf(n.getPrice())); }
if(e.getSource()==button4) // 修改 {
//定义一个空的users对象
10 / 32
报刊订阅管理系统
newspaper news=new newspaper(); //将数据封装到u当中
news.setNewsNo(text1.getText().trim()); news.setNewsName(text2.getText().trim()); news.setPublish(text3.getText().trim()); news.setPubPeriod(text4.getText().trim()); news.setContent(text5.getText().trim());
news.setPrice(Float.parseFloat(text6.getText().trim())); //定义一个控制对象
InformationDaoImpl ifd=new InformationDaoImpl(); //执行修改用户信息操作 ifd.updateNewspaper(news); System.out.println(news); } }
(2)InformationDaoImpl.java //添加报刊
public void addNews(newspaper n){ //定义一个空的连接对象 Connection conn=null; //定义一个准备sql语句 PreparedStatement ps=null; //自定义将要执行的sql语句 String sql=\newspaper(newsNo,newsName,publish,pubPeriod,content,price) values(?,?,?,?,?,?)\
//通过Dbutils得到数据库的连接 conn=Dbutils.getConnection(); System.out.println(conn); try {
//将sql语句传给ps(接收sql语句的容器) ps=conn.prepareStatement(sql); //将news的各个属性值添加到?处 ps.setString(1, n.getNewsNo()); ps.setString(2, n.getNewsName()); ps.setString(3, n.getPublish()); ps.setString(4, n.getPubPeriod()); ps.setString(5, n.getContent()); ps.setFloat(6, n.getPrice());
//执行sql语句(相当于在数据库中执行sql语句) ps.executeUpdate(); } catch (SQLException e) {
// TODO 自动生成的 catch 块
11 / 32
into 报刊订阅管理系统
e.printStackTrace(); }
finally{
//释放连接,同时释放资源
Dbutils.close(conn, ps, null); } }
//通过报刊号和报刊名删除报刊
public void deleteNews(newspaper dn) { // TODO 自动生成的方法存根 Connection conn=null;
PreparedStatement ps=null; conn=Dbutils.getConnection(); String sql=\from newspaper where newsNo=? and newsName=?\ try {
ps=conn.prepareStatement(sql); ps.setString(1, dn.getNewsNo()); ps.setString(2, dn.getNewsName()); ps.executeUpdate(); } catch (SQLException e) {
// TODO 自动生成的 catch 块 e.printStackTrace(); }finally {
Dbutils.close(conn, ps, null); } }
//通过报刊代号查找报刊
public newspaper FindNewsByNewspaper(String ne) { newspaper news= new newspaper(); Connection conn=null;
PreparedStatement ps=null; ResultSet rs=null;
conn=Dbutils.getConnection();
String sql=\ try {
ps=conn.prepareStatement(sql); ps.setString(1, ne); rs=ps.executeQuery(); if(rs.next()) {
news.setNewsNo(rs.getString(\
12 / 32
报刊订阅管理系统
news.setNewsName(rs.getString(\ news.setPublish(rs.getString(\
news.setPubPeriod(rs.getString(\ news.setContent(rs.getString(\ news.setPrice(rs.getFloat(\ }
} catch (SQLException e) {
// TODO 自动生成的 catch 块 e.printStackTrace(); }finally {
Dbutils.close(conn, ps, rs); }
return news;
//更新报刊信息
public void updateNewspaper(newspaper news) { // TODO Auto-generated method stub //定义一个空的连接对象 Connection conn=null; //定义一个准备sql语句 PreparedStatement ps=null; //自定义将要执行的sql语句
String sql=\newspaper set publish=? content=?, price=? where newsNo=? and newsName=? \ //通过Dbutils得到数据库的连接 conn=Dbutils.getConnection(); System.out.println(conn); try {
//将sql语句传给ps(接收sql语句的容器) ps=conn.prepareStatement(sql); //将user的各个属性值添加到?处 ps.setString(1, news.getPublish()); ps.setString(2, news.getPubPeriod()); ps.setString(3, news.getContent()); ps.setFloat(4, news.getPrice()); ps.setString(5,news.getNewsNo()); ps.setString(6, news.getNewsName()); //执行update SQL语句 ps.executeUpdate(); } catch (SQLException e) {
// TODO 自动生成的 catch 块 e.printStackTrace(); }
13 / 32
,pubPeriod=?,