我有5个班级。
访问studentController
时,表中包含信息。我正在将数据添加到另一个表中的表中,并且继续保存该表,但是它不会更新该表
学生班
package sample;
public class Student {
String name;
String amail;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAmail() {
return amail;
}
public void setAmail(String amail) {
this.amail = amail;
}
public Student(String name, String amail) {
this.name = name;
this.amail = amail;
}
}
DBConnection类
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
public Connection connection;
String dbname="student";
String username="root";
String password="";
public java.sql.Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection= DriverManager.getConnection("jdbc:mysql://localhost/"+dbname,username,password );
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
DBFunc类
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBfunc {
public void insertbolim(String name,String amail) throws SQLException {
DBConnection dbConnection= new DBConnection();
Connection connection=dbConnection.getConnection();
String sql="INSERT INTO student (name,amail) VALUES ('"+name+"','"+amail+"')";
Statement statement= null;
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
statement.executeUpdate(sql);
}
public ObservableList selectbolim() throws SQLException {
ObservableList<Student> nomi=FXCollections.observableArrayList();
DBConnection db =new DBConnection();
Connection connection =db.getConnection();
Statement statement =connection.createStatement();
String sql="SELECT * FROM `student`";
ResultSet rs=statement.executeQuery(sql);
while(rs.next())
{
nomi.add(new Student(rs.getString(2),rs.getString(3)));
}
return nomi;
}
}
ControllerStudents类
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class ControllerStudents implements Runnable{
@FXML
private TableColumn<Student, String> colname;
@FXML
private TableColumn<Student, String> colamail;
@FXML
private TableView<Student> tbview;
@FXML
void initialize() {
Refresh();
}
@Override
public void run() {
Refresh();
}
public void Refresh(){
DBfunc dBfunc = new DBfunc();
colname.setCellValueFactory(new PropertyValueFactory<Student,String>("name"));
colamail.setCellValueFactory(new PropertyValueFactory<Student,String>("amail"));
try {
tbview.setItems(dBfunc.selectbolim());
} catch (SQLException e) {
e.printStackTrace();
}
}
@FXML
void addstudentaction(ActionEvent event) {
AnchorPane pane = null;
try {
pane = FXMLLoader.load(getClass().getResource("../sample/AddStudents.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Stage stage =new Stage();
stage.setTitle("Add Students");
stage.setResizable(false);
stage.setMaximized(false);
stage.setScene(new Scene(pane));
stage.show();
}
}
AddStudent类
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import java.sql.SQLException;
public class AddStudents {
@FXML
private TextField txtname;
@FXML
private TextField txtamail;
@FXML
void saveaction(ActionEvent event) {
DBfunc dBfunc = new DBfunc();
try {
dBfunc.insertbolim(txtname.getText(),txtamail.getText());
} catch (SQLException e) {
e.printStackTrace();
}
ControllerStudents cs=new ControllerStudents();
Thread thread = new Thread(cs);
thread.start();
}}
Exception in thread "Thread-4" java.lang.NullPointerException
at sample.ControllerStudents.Refresh(ControllerStudents.java:52)
at sample.ControllerStudents.run(ControllerStudents.java:45)
at java.lang.Thread.run(Thread.java:748)