[Java] 多表增删改查 →→→→→进入此内容的聊天室

来自 , 2020-11-24, 写在 Java, 查看 127 次.
URL http://www.code666.cn/view/5e1b18c4
  1. package com.ww.service;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.sql.Connection;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8.  
  9. import com.mysql.jdbc.PreparedStatement;
  10. import com.ww.db.DBHelper;
  11. import com.ww.entity.Score;
  12. import com.ww.entity.Student;
  13.  
  14. public class SysService {
  15.  
  16.         DBHelper dbHelper=new DBHelper();
  17.         //查询所有学生的编号,姓名和分数,并返回
  18.         public ArrayList<Score> getAllInfo(){
  19.                 String sql="select * from stu s,sco sc where s.stu_id=sc.stu_id";
  20.                 Connection connection=dbHelper.getConnection();
  21.                 ArrayList<Score> scores=new ArrayList<Score>();
  22.                 try {
  23.                         PreparedStatement psd=(PreparedStatement)connection.prepareStatement(sql);
  24.                         ResultSet resultSet=psd.executeQuery();
  25.                        
  26.                         while (resultSet.next()) {
  27.                                 Student student=new Student();
  28.                                 Score score=new Score();
  29.                                 student.setId(resultSet.getInt("stu_id"));
  30.                                 student.setName(resultSet.getString("stu_name"));
  31.                                
  32.                                 score.setId(resultSet.getInt("sco_id"));
  33.                                 score.setScos(resultSet.getInt("emg_sco"));
  34.                                 score.setStudent(student);
  35.                                 scores.add(score);
  36.                         }
  37.                 } catch (SQLException e) {
  38.                         // TODO Auto-generated catch block
  39.                         e.printStackTrace();
  40.                 }
  41.                 return scores;
  42.         }
  43.  
  44.         //添加
  45.         public boolean addInfo(String name,int a){
  46.                 String sql1="insert into stu VALUES(null,?)";
  47.                 String sql2="insert into sco VALUES(null,(select MAX(stu_id) from stu),?)";
  48.                 Connection connection=dbHelper.getConnection();
  49.                 try {
  50.                         //执行向主表添加数据
  51.                         PreparedStatement psd1=(PreparedStatement)connection.prepareStatement(sql1);
  52.                         //执行向从表中添加数据
  53.                         PreparedStatement psd2=(PreparedStatement)connection.prepareStatement(sql2);
  54.                         psd1.setString(1, name);
  55.                         int b=psd1.executeUpdate();
  56.                         if (b>0) {
  57.                                 //向主表中完成添加
  58.                                 psd2.setInt(1, a);
  59.                                 int s=psd2.executeUpdate();
  60.                                 if (s>0) {
  61.                                         return true;
  62.                                 }
  63.                         }
  64.                 } catch (SQLException e) {
  65.                         // TODO Auto-generated catch block
  66.                         e.printStackTrace();
  67.                 }
  68.                 return false;
  69.         }
  70.        
  71.        
  72.         //修改
  73.         public boolean update(int id,String name,int sc) {
  74.                 String sql="update stu s,sco sc set s.stu_name=?,sc.emg_sco=? where s.stu_id=sc.stu_id and s.stu_id=?";
  75.                 Connection connection=dbHelper.getConnection();
  76.                 try {
  77.                         PreparedStatement psd=(PreparedStatement)connection.prepareStatement(sql);
  78.                         psd.setString(1, name);
  79.                         psd.setInt(2, sc);
  80.                         psd.setInt(3, id);
  81.                         if(psd.executeUpdate()>=2){
  82.                                 return true;
  83.                         }
  84.                 } catch (SQLException e) {
  85.                         // TODO Auto-generated catch block
  86.                         e.printStackTrace();
  87.                 }
  88.                 return false;
  89.         }
  90.        
  91.        
  92.        
  93.        
  94.         //删除
  95.         public boolean deleteInfo(int id){
  96.                 String sql="delete from sco where stu_id=?";
  97.                 Connection connection=dbHelper.getConnection();
  98.                 try {
  99.                         PreparedStatement psd=(PreparedStatement)connection.prepareStatement(sql);
  100.                         psd.setInt(1, id);
  101.                         if(psd.executeUpdate()>0){
  102.                                 return true;
  103.                         }
  104.                 } catch (SQLException e) {
  105.                         // TODO Auto-generated catch block
  106.                         e.printStackTrace();
  107.                 }
  108.                 return false;
  109.         }
  110. }
  111.  

回复 "多表增删改查"

这儿你可以回复上面这条便签

captcha