标签:
1 package gui; 2 3 import action.C2SAction; 4 import action.CourseAction; 5 import action.StudentAction; 6 import javafx.application.Application; 7 import javafx.geometry.Insets; 8 import javafx.scene.Scene; 9 import javafx.scene.control.*; 10 import javafx.scene.layout.GridPane; 11 import javafx.scene.layout.HBox; 12 import javafx.stage.Stage; 13 import model.C2S; 14 import model.Course; 15 import model.Student; 16 17 import java.sql.SQLException; 18 import java.util.List; 19 20 /** 21 * Created by yinus 22 * Date:2016/4/16 23 * Time:13:45 24 */ 25 public class view extends Application { 26 @Override 27 public void start(Stage primaryStage) throws Exception { 28 HBox box = new HBox(); 29 box.setSpacing(10); 30 TextArea textArea = new TextArea("second database project\n"); 31 textArea.appendText("------version 1.0------\n"); 32 textArea.appendText("------author ycy------\n"); 33 textArea.setPrefColumnCount(50); 34 textArea.setPrefRowCount(20); 35 textArea.setWrapText(true); 36 textArea.setEditable(false); 37 ScrollPane scrollPane = new ScrollPane(textArea); 38 GridPane gridPane = new GridPane(); 39 gridPane.setPadding(new Insets(5,5,5,5)); 40 gridPane.setHgap(10); 41 gridPane.setVgap(10); 42 gridPane.add(new Label("student name"),0,0); 43 TextField sn = new TextField(); 44 sn.setMaxWidth(100); 45 gridPane.add(sn,1,0); 46 gridPane.add(new Label("student id"),0,1); 47 TextField si = new TextField(); 48 si.setMaxWidth(100); 49 gridPane.add(si,1,1); 50 gridPane.add(new Label("course title"),0,2); 51 TextField ct = new TextField(); 52 ct.setMaxWidth(100); 53 gridPane.add(ct,1,2); 54 gridPane.add(new Label("course id"),0,3); 55 TextField ci = new TextField(); 56 ci.setMaxWidth(100); 57 gridPane.add(ci,1,3); 58 gridPane.add(new Label("credit"),0,4); 59 TextField cd = new TextField(); 60 cd.setMaxWidth(100); 61 gridPane.add(cd,1,4); 62 RadioButton rs = new RadioButton("student"); 63 RadioButton rc = new RadioButton("course"); 64 RadioButton rcs = new RadioButton("cs"); 65 gridPane.add(rcs,0,5); 66 gridPane.add(rc,1,5); 67 gridPane.add(rs,2,5); 68 ToggleGroup group = new ToggleGroup(); 69 rs.setToggleGroup(group); 70 rc.setToggleGroup(group); 71 rcs.setToggleGroup(group); 72 Button add = new Button("add"); 73 Button delete = new Button("delete"); 74 Button update = new Button("update"); 75 Button query = new Button("query"); 76 Button clear = new Button("clear"); 77 gridPane.add(add,0,6); 78 gridPane.add(delete,1,6); 79 gridPane.add(update,0,7); 80 gridPane.add(query,1,7); 81 gridPane.add(clear,0,8); 82 box.getChildren().addAll(scrollPane,gridPane); 83 Scene scene = new Scene(box); 84 primaryStage.setScene(scene); 85 primaryStage.setTitle("dbproj1.0"); 86 primaryStage.show(); 87 rcs.setSelected(true); 88 clear.setOnAction(e->{ 89 sn.clear(); 90 si.clear(); 91 ci.clear(); 92 cd.clear(); 93 ct.clear(); 94 }); 95 add.setOnAction(e->{ 96 if (rc.isSelected()){ 97 String courseTitle = ct.getText(); 98 int courseId = Integer.parseInt(ci.getText()); 99 CourseAction courseAction = new CourseAction(); 100 try { 101 courseAction.add(new Course(courseId,courseTitle)); 102 textArea.appendText("title:"+courseTitle+" id:"+courseId+ " added successfully\n" ); 103 104 } catch (SQLException e1) { 105 e1.printStackTrace(); 106 } 107 } 108 else if (rs.isSelected()){ 109 String studentName = sn.getText(); 110 int stuId = Integer.parseInt(si.getText()); 111 StudentAction studentAction = new StudentAction(); 112 try { 113 studentAction.add(new Student(stuId,studentName)); 114 textArea.appendText("name:"+studentName+" id:"+stuId+ " added successfully\n" ); 115 116 } catch (SQLException e1) { 117 e1.printStackTrace(); 118 } 119 } 120 else if (rcs.isSelected()){ 121 int stuid = Integer.parseInt(si.getText()); 122 int couid = Integer.parseInt(ci.getText()); 123 double cr = Double.parseDouble(cd.getText()); 124 C2SAction c2SAction = new C2SAction(); 125 try { 126 c2SAction.add(new C2S(stuid,couid,cr)); 127 textArea.appendText("student id:"+stuid+" course id:"+couid+" credit"+ cr + " added successfully\n" ); 128 129 } catch (SQLException e1) { 130 e1.printStackTrace(); 131 } 132 } 133 }); 134 135 delete.setOnAction(e->{ 136 if (rc.isSelected()){ 137 int i = Integer.parseInt(ci.getText()); 138 CourseAction courseAction = new CourseAction(); 139 try { 140 courseAction.delete(i); 141 textArea.appendText("delete successfully\n"); 142 } catch (SQLException e1) { 143 e1.printStackTrace(); 144 } 145 } 146 else if (rs.isSelected()){ 147 int i = Integer.parseInt(si.getText()); 148 StudentAction studentAction = new StudentAction(); 149 try { 150 studentAction.delete(i); 151 textArea.appendText("delete successfully\n"); 152 } catch (SQLException e1) { 153 e1.printStackTrace(); 154 } 155 } 156 else if (rcs.isSelected()){ 157 int c = Integer.parseInt(ci.getText()); 158 int s = Integer.parseInt(si.getText()); 159 C2SAction cs = new C2SAction(); 160 try { 161 cs.delete(s,c); 162 textArea.appendText("delete successfully\n"); 163 } catch (SQLException e1) { 164 e1.printStackTrace(); 165 } 166 } 167 }); 168 169 update.setOnAction(e->{ 170 if (rc.isSelected()){ 171 String courseTitle = ct.getText(); 172 int courseId = Integer.parseInt(ci.getText()); 173 CourseAction courseAction = new CourseAction(); 174 try { 175 Course course = courseAction.queryById(courseId); 176 course.setTitle(courseTitle); 177 courseAction.update(course); 178 textArea.appendText("title:"+courseTitle+" id:"+courseId+ " updated successfully\n" ); 179 } catch (SQLException e1) { 180 e1.printStackTrace(); 181 } 182 } 183 else if (rs.isSelected()){ 184 String studentName = sn.getText(); 185 int stuId = Integer.parseInt(si.getText()); 186 StudentAction studentAction = new StudentAction(); 187 try { 188 Student student = studentAction.queryById(stuId); 189 student.setName(studentName); 190 studentAction.update(student); 191 textArea.appendText("name:"+studentName+" id:"+stuId+ " updated successfully\n" ); 192 } catch (SQLException e1) { 193 e1.printStackTrace(); 194 } 195 196 } 197 else if (rcs.isSelected()){ 198 int stuid = Integer.parseInt(si.getText()); 199 int couid = Integer.parseInt(ci.getText()); 200 double cr = Double.parseDouble(cd.getText()); 201 C2SAction c2SAction = new C2SAction(); 202 try { 203 C2S s = c2SAction.query(stuid, couid); 204 s.setCredit(cr); 205 c2SAction.update(s); 206 textArea.appendText("student id:"+stuid+" course id:"+couid+" credit"+ cr + " updated successfully\n" ); 207 } catch (SQLException e1) { 208 e1.printStackTrace(); 209 } 210 } 211 }); 212 213 query.setOnAction(e->{ 214 CourseAction courseAction = new CourseAction(); 215 if (rc.isSelected()){ 216 if (!ci.getText().equals("")) { 217 try { 218 int id = Integer.parseInt(ci.getText()); 219 Course course = courseAction.queryById(id); 220 if (course == null) 221 textArea.appendText("null\n"); 222 else 223 textArea.appendText(course.toString()); 224 } catch (SQLException e1) { 225 e1.printStackTrace(); 226 } catch (NumberFormatException ee) { 227 ee.printStackTrace(); 228 } 229 } 230 else if (!ct.getText().equals("")){ 231 String courseTitle = ct.getText().trim(); 232 try { 233 List<Course> courses = courseAction.queryByName(courseTitle); 234 courses.forEach(course -> textArea.appendText(course.toString())); 235 } catch (SQLException e1) { 236 e1.printStackTrace(); 237 } 238 } 239 } 240 else if (rs.isSelected()){ 241 StudentAction studentAction = new StudentAction(); 242 if (!si.getText().equals("")){ 243 int i = Integer.parseInt(si.getText()); 244 try { 245 Student student = studentAction.queryById(i); 246 textArea.appendText(student.toString()); 247 } catch (SQLException e1) { 248 e1.printStackTrace(); 249 } 250 } 251 else if (!sn.getText().equals("")){ 252 try { 253 List<Student> students = studentAction.queryByName(sn.getText()); 254 students.forEach(student -> textArea.appendText(student.toString())); 255 } catch (SQLException e1) { 256 e1.printStackTrace(); 257 } 258 } 259 } 260 else if (rcs.isSelected()){ 261 int stuid = Integer.parseInt(si.getText()); 262 int couid = Integer.parseInt(ci.getText()); 263 C2SAction cs = new C2SAction(); 264 try { 265 C2S c2S = cs.query(stuid, couid); 266 textArea.appendText(c2S.toString()); 267 } catch (SQLException e1) { 268 e1.printStackTrace(); 269 } 270 } 271 }); 272 } 273 public static void main(String[] args) { 274 Application.launch(args); 275 } 276 }
标签:
原文地址:http://www.cnblogs.com/ycy1025/p/5399195.html