如何添加记录,更新记录,删除记录,使用mysql jdbc在javafx netbeans中使用场景构建器在tableview中搜索记录... 以下是我如何插入,更新,删除和搜索到mysql jdbc的代码...但不是表因为我现在正面临的问题(插入新记录,更新它们,删除它们并搜索使用场景构建器在javafx netbeans中的tableview中的记录
//mycotroller...
package fxapplication;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javax.swing.JOptionPane;
/**
* FXML Controller class
*
* @author PHILSERVER
*/
public class SearchController implements Initializable {
@FXML
private Connection con;
@FXML
private Statement stmt;
@FXML
private ResultSet rs;
@FXML
private ComboBox cbGender;
@FXML
private TextField tfLocation;
@FXML
private TextField tfFirstname;
@FXML
private TextField tfSearch;
@FXML
private Button update;
@FXML
private Button insert;
@FXML
private Button delete;
@FXML
private TextField tfSecondname;
@FXML
void search(ActionEvent event) {
if (tfSearch.getText().equals("")){
JOptionPane.showMessageDialog(null," Enter firstname please !!!");
}else{
if(checkName()){
search();
}else{
JOptionPane.showMessageDialog(null,"firstname does not exist");
}
}
}
@FXML
void deleteRecord(ActionEvent event) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
stmt = con.createStatement();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
}
try{
String sql;
sql="delete from sstudent where firstname='"+tfSearch.getText()+"'";
int result=stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null," Record deleted sucessfully");
tfSearch.setText(null);
tfFirstname.setText(null);
tfSecondname.setText(null);
cbGender.setValue(null);
tfLocation.setText(null);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"Unable to delete record "+e.getMessage());
}
}
@FXML
void updateRecord(ActionEvent event) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
stmt = con.createStatement();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
}
try{
String sql;
sql="update sstudent set secondname='"+tfSecondname.getText()+"',"
+"location='"+tfLocation.getText()+"',"
+"gender='"+cbGender.getValue().toString()+"'"
+" where firstname='"+tfSearch.getText()+"'";
int result=stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null," Record updated sucessfully");
} catch (SQLException f) {
JOptionPane.showMessageDialog(null,"Unable to update record "+f.getMessage());
}
}
@FXML
void insertRecord(ActionEvent event) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
stmt=con.createStatement();
}catch (SQLException e){
JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
}
try {
String sql;
sql="insert into sstudent(firstname,secondname,gender,location) values ("
+"'"+tfFirstname.getText()+"',"
+"'"+tfSecondname.getText()+"',"
+"'"+cbGender.getValue().toString()+"',"
+"'"+tfLocation.getText()+"')";
int result=stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null,"record saved sucessfully ");
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Unable to insert record "+e.getMessage());
}
}
private boolean checkName(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
stmt = con.createStatement();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
}
try{
String sql;
sql="select * from sstudent where firstname='"+tfSearch.getText()+"'";
rs=stmt.executeQuery(sql);
if(rs.next()){
return true;
}
}catch (SQLException e){
JOptionPane.showMessageDialog(null,"unable to retrieve record"+ e.getMessage());
}
return false;
}
private void search(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
stmt = con.createStatement();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
}
try {
String sql;
sql = "select * from sstudent where firstname='"+tfSearch.getText()+"'";
rs = stmt.executeQuery(sql);
while(rs.next()){
tfFirstname.setText(rs.getString("firstname"));
tfSecondname.setText(rs.getString("secondname"));
cbGender.setValue(rs.getString("gender"));
tfLocation.setText(rs.getString("location"));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"Unable to retrieve record "+e.getMessage());
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
cbGender.getItems().addAll("male","female");
}
}
//main class....
package fxapplication;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
/**
*
* @author PHILSERVER
*/
public class FXApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("search.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
// my scene builder forms....
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="fxapplication.SearchController">
<children>
<Pane layoutX="24.0" layoutY="23.0" prefHeight="347.0" prefWidth="549.0" style="-fx-background-color: green;">
<children>
<TextField fx:id="tfSearch" layoutX="187.0" layoutY="16.0" prefHeight="39.0" prefWidth="160.0" />
<Label layoutX="33.0" layoutY="27.0" prefHeight="17.0" prefWidth="75.0" text="firstname" />
<Button layoutX="366.0" layoutY="20.0" mnemonicParsing="false" onAction="#search" prefHeight="31.0" prefWidth="103.0" text="search" />
<Button fx:id="delete" layoutX="342.0" layoutY="308.0" mnemonicParsing="false" onAction="#deleteRecord" prefHeight="25.0" prefWidth="103.0" text="delete" />
<Button fx:id="update" layoutX="185.0" layoutY="308.0" mnemonicParsing="false" onAction="#updateRecord" prefHeight="25.0" prefWidth="103.0" text="update" />
<Button fx:id="insert" layoutX="33.0" layoutY="308.0" mnemonicParsing="false" onAction="#insertRecord" prefHeight="25.0" prefWidth="103.0" text="insert" />
<Label layoutX="33.0" layoutY="76.0" prefHeight="17.0" prefWidth="60.0" text="firstname" />
<Label layoutX="33.0" layoutY="125.0" prefHeight="17.0" prefWidth="75.0" text="secondname" />
<Label layoutX="33.0" layoutY="174.0" prefHeight="17.0" prefWidth="60.0" text="gender" />
<Label layoutX="33.0" layoutY="222.0" prefHeight="17.0" prefWidth="60.0" text="location" />
<TextField fx:id="tfFirstname" layoutX="187.0" layoutY="72.0" prefHeight="25.0" prefWidth="169.0" />
<TextField fx:id="tfSecondname" layoutX="186.0" layoutY="121.0" prefHeight="25.0" prefWidth="169.0" />
<TextField fx:id="tfLocation" layoutX="187.0" layoutY="218.0" prefHeight="25.0" prefWidth="169.0" />
<ComboBox fx:id="cbGender" layoutX="185.0" layoutY="170.0" prefHeight="25.0" prefWidth="169.0" promptText="<select gender>" />
</children>
</Pane>
</children>
</AnchorPane>