建立一个private类型的DatabassCnonection函数实现与数据库的连接,在start方法中进行调用,即可完成数据库的连接。注:进行数据库连接时记得修改自己root密码,和数据库名称,以及表名。
private void DatabassConnection() {
try{ Class.forName("com.mysql.cj.jdbc.Driver");
String s="insert into student values(?,?,?,?)";
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/study?useSSL=false&characterEncoding=utf8&serverTimezone=UTC&allowPublicKeyRetrieval=true","root","123456");
state=conn.prepareStatement(s);
String s2="select *from student where name=? and course=?";
state2=conn.prepareStatement(s2);
}
catch(ClassNotFoundException | SQLException e){
System.out.println(e.getMessage());}
}
//按钮
Button bt_insert = new Button("录入");
Button bt_select = new Button("查找");
//边界面板
BorderPane bd_pane = new BorderPane();
//各标签的文本框
TextArea center_text=new TextArea();
Label lb_id = new Label("学号");
Label lb_name = new Label("姓名");
Label lb_name2 = new Label("姓名");
Label lb_course = new Label("课程名");
Label lb_course2 = new Label("课程名");
Label lb_grade = new Label("成绩");
@Override
public void start(Stage stage) throws Exception,SQLException {
DatabassConnection();
//建立静态页面
BorderPane bdpane = new BorderPane();
HBox myHbox1 = new HBox();
myHbox1.setSpacing(5);
myHbox1.setAlignment(Pos.CENTER);
Insets insets=new Insets(10,10,10,10);
Insets insets2=new Insets(1,1,1,1);
VBox myvbox1 = new VBox();
myvbox1.setAlignment(Pos.CENTER_LEFT);
//myHbox.setFillHeight(true);
//学号
TextArea text_id = new TextArea();
text_id.setPrefRowCount(1);
text_id.setPrefColumnCount(10);
//姓名
TextArea text_name=new TextArea();
text_name.setPrefRowCount(1);
text_name.setPrefColumnCount(10);
//课程名
TextArea text_course=new TextArea();
text_course.setPrefRowCount(1);
text_course.setPrefColumnCount(10);
//成绩
TextArea text_grade=new TextArea();
text_grade.setPrefRowCount(1);
text_grade.setPrefColumnCount(10);
//额外的text
TextArea text_name2=new TextArea();
text_name2.setPrefRowCount(1);
text_name2.setPrefColumnCount(5);
TextArea text_course2=new TextArea();
text_course2.setPrefRowCount(1);
text_course2.setPrefColumnCount(5);
//top
myHbox1.setMargin(bt_insert,insets);
myHbox1.setMargin(lb_id,insets);
myHbox1.setMargin(text_id,insets);
myHbox1.setMargin(lb_course,insets);
myHbox1.setMargin(text_course,insets);
myHbox1.setMargin(lb_name,insets);
myHbox1.setMargin(text_name,insets);
myHbox1.setMargin(lb_grade,insets);
myHbox1.setMargin(text_grade,insets);
ObservableList list1 = myHbox1.getChildren();
list1.addAll(bt_insert,lb_id,text_id,lb_name,text_name,lb_course,text_course,lb_grade,text_grade);
//左边界
myvbox1.setMargin(bt_select,insets2);
myvbox1.setMargin(lb_name2,insets2);
myvbox1.setMargin(text_name2,insets2);
myvbox1.setMargin(lb_course2,insets2);
myvbox1.setMargin(text_course2,insets2);
ObservableList list2 = myvbox1.getChildren();
list2.addAll(bt_select,lb_name2,text_name2,lb_course2,text_course2);
//中间文本显示框
bdpane.setTop(myHbox1);
bdpane.setLeft(myvbox1);
bdpane.setCenter(center_text);
Scene scene = new Scene(bdpane);
stage.setScene(scene);
stage.show();
}
bt_select.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
String name2=text_name2.getText();
String course2=text_course2.getText();
try {
state2.setString(1,name2);
state2.setString(2,course2);
rs = state2.executeQuery();
center_text.setText("id name course grade"+"\n");
while (rs.next()){
center_text.appendText(rs.getString("id")+" ");
center_text.appendText(rs.getString("name")+" ");
center_text.appendText(rs.getString("course")+" ");
center_text.appendText(rs.getInt("grade")+"\n");
System.out.print(rs.getString("id")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("course")+" ");
System.out.println(rs.getInt("grade")+" ");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
注:记得修改自己的root密码和数据库名以及表名
package com.example.fxdemo;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.ImagePattern;
import javafx.stage.Stage;
import java.sql.*;
//https://www.cnblogs.com/jffx/p/9911968.html
//1、编写一个能够进行异常处理的分数管理小系统。
public class synthesize extends Application {
//private Connection con=null;
private ResultSet rs;
private PreparedStatement state;
private PreparedStatement state2;
private void DatabassConnection() {
try{ Class.forName("com.mysql.cj.jdbc.Driver");
String s="insert into student values(?,?,?,?)";
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/study?useSSL=false&characterEncoding=utf8&serverTimezone=UTC&allowPublicKeyRetrieval=true","root","123456");
state=conn.prepareStatement(s);
String s2="select *from student where name=? and course=?";
state2=conn.prepareStatement(s2);
}
catch(ClassNotFoundException | SQLException e){
System.out.println(e.getMessage());}
}
//按钮
Button bt_insert = new Button("录入");
Button bt_select = new Button("查找");
//边界面板
BorderPane bd_pane = new BorderPane();
//各标签的文本框
TextArea center_text=new TextArea();
Label lb_id = new Label("学号");
Label lb_name = new Label("姓名");
Label lb_name2 = new Label("姓名");
Label lb_course = new Label("课程名");
Label lb_course2 = new Label("课程名");
Label lb_grade = new Label("成绩");
@Override
public void start(Stage stage) throws Exception,SQLException {
DatabassConnection();
//建立静态页面
BorderPane bdpane = new BorderPane();
HBox myHbox1 = new HBox();
myHbox1.setSpacing(5);
myHbox1.setAlignment(Pos.CENTER);
Insets insets=new Insets(10,10,10,10);
Insets insets2=new Insets(1,1,1,1);
VBox myvbox1 = new VBox();
myvbox1.setAlignment(Pos.CENTER_LEFT);
//myHbox.setFillHeight(true);
//学号
TextArea text_id = new TextArea();
text_id.setPrefRowCount(1);
text_id.setPrefColumnCount(10);
//姓名
TextArea text_name=new TextArea();
text_name.setPrefRowCount(1);
text_name.setPrefColumnCount(10);
//课程名
TextArea text_course=new TextArea();
text_course.setPrefRowCount(1);
text_course.setPrefColumnCount(10);
//成绩
TextArea text_grade=new TextArea();
text_grade.setPrefRowCount(1);
text_grade.setPrefColumnCount(10);
//额外的text
TextArea text_name2=new TextArea();
text_name2.setPrefRowCount(1);
text_name2.setPrefColumnCount(5);
TextArea text_course2=new TextArea();
text_course2.setPrefRowCount(1);
text_course2.setPrefColumnCount(5);
//top
myHbox1.setMargin(bt_insert,insets);
myHbox1.setMargin(lb_id,insets);
myHbox1.setMargin(text_id,insets);
myHbox1.setMargin(lb_course,insets);
myHbox1.setMargin(text_course,insets);
myHbox1.setMargin(lb_name,insets);
myHbox1.setMargin(text_name,insets);
myHbox1.setMargin(lb_grade,insets);
myHbox1.setMargin(text_grade,insets);
ObservableList list1 = myHbox1.getChildren();
list1.addAll(bt_insert,lb_id,text_id,lb_name,text_name,lb_course,text_course,lb_grade,text_grade);
//左边界
myvbox1.setMargin(bt_select,insets2);
myvbox1.setMargin(lb_name2,insets2);
myvbox1.setMargin(text_name2,insets2);
myvbox1.setMargin(lb_course2,insets2);
myvbox1.setMargin(text_course2,insets2);
ObservableList list2 = myvbox1.getChildren();
list2.addAll(bt_select,lb_name2,text_name2,lb_course2,text_course2);
//响应事件,录入信息
bt_insert.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
String id=text_id.getText();
String name=text_name.getText();
String course=text_course.getText();
String grade=text_grade.getText();
int grade1=Integer.parseInt(grade);
try {
state.setString(1,id);
state.setString(2,name);
state.setString(3,course);
state.setInt(4,grade1);
state.executeUpdate();
center_text.setText("录入成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
});
//响应事件,查询信息
bt_select.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
String name2=text_name2.getText();
String course2=text_course2.getText();
try {
state2.setString(1,name2);
state2.setString(2,course2);
rs = state2.executeQuery();
center_text.setText("id name course grade"+"\n");
while (rs.next()){
center_text.appendText(rs.getString("id")+" ");
center_text.appendText(rs.getString("name")+" ");
center_text.appendText(rs.getString("course")+" ");
center_text.appendText(rs.getInt("grade")+"\n");
System.out.print(rs.getString("id")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("course")+" ");
System.out.println(rs.getInt("grade")+" ");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
//中间文本显示框
bdpane.setTop(myHbox1);
bdpane.setLeft(myvbox1);
bdpane.setCenter(center_text);
Scene scene = new Scene(bdpane);
stage.setScene(scene);
stage.show();
}
public static void main(String args[]) throws SQLException, ClassNotFoundException {
launch(args);
}}