JDBC 第一个使用数据库写的软件

程序小白,希望和大家多交流,共同学习

//1.创建数据库
//2.查找,通过输入学号,点击find按钮,查找学生信息,close按钮断开数据库连接。
//断开连接后无法再访问数据。重启后连接再次建立

//数据库创建
CREATE DATABASE student;
USE student;
CREATE TABLE student(
    sno INT
    ,name VARCHAR(15)
    ,gender VARCHAR(6)
    ,cno SMALLINT
    ,post VARCHAR(10)
    ,age SMALLINT
    ,PRIMARY KEY(sno)
    );

INSERT INTO student(sno, name, gender, cno, post, age)
    VALUES(801, '张三', 'femal', 1, 802, 19);

INSERT INTO student(sno, name, gender, cno, post, age)
    VALUES(802, '李四', 'mal', 1, NULL, 20);

INSERT INTO student(sno, name, gender, cno, post, age)
    VALUES(803, '王五', 'mal', 1, 802, 20);

INSERT INTO student(sno, name, gender, cno, post, age)
    VALUES(804, '赵六', 'femal', 2, 805, 20);

INSERT INTO student(sno, name, gender, cno, post, age)
    VALUES(805, '钱七', 'mal', 2, NULL, 19);
//访问数据库的类
//这个类写的很具体,只能访问student数据库
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class VisitDatabase
{
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;

    public VisitDatabase()
    {
        findInfo();
    }

    public void findInfo()
    {
        try
        {
            //创建驱动
            Class.forName("com.mysql.jdbc.Driver");
            //建立连接
            connection = DriverManager.getConnection(
                "jdbc:mysql://localhost/student", "root", "021191");
            //创建语句
            statement = connection.createStatement();
        }
        catch (SQLException ex)
        {
            System.out.println(ex);
        }
        catch (ClassNotFoundException ex)
        {
            System.out.println(ex);
        }
    }
    //按值查找数据库
    public String[] getInfo(String sno) throws SQLException
    {
        String[] result = new String[5];
        //执行语句
        resultSet = statement.executeQuery(
            "SELECT name, gender, age, post, cno FROM student WHERE SNO=" + sno + ";");
        if (resultSet.next())
        {//name gender age post cno
            result[0] = resultSet.getString("name");
            result[1] = resultSet.getString("gender");
            result[2] = resultSet.getString("age");
            result[3] = resultSet.getString("post");
            result[4] = resultSet.getString("cno");
        }

        return result;
    }
    //关闭数据库
    public void close()
    {
        try
        {
            if (connection != null)
            {
                connection.close();
            }
            if (statement != null)
            {
                statement.close();
            }
            if (resultSet != null)
            {
                resultSet.close();
            }
        }
        catch (SQLException ex)
        {
            System.out.println(ex);
        }
    }
}
//访问数据库
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import java.sql.SQLException;

public class FindStudentFromSQL extends Application
{
    //私有插件
    private TextField tfSno = new TextField();
    private TextField tfName = new TextField();
    private TextField tfGender = new TextField();
    private TextField tfCno = new TextField();
    private TextField tfPost = new TextField();
    private TextField tfAge = new TextField();
    private Button btFind = new Button("find");
    private Button btClose = new Button("close");
    private boolean close = false;
    //数据库访问对象
    VisitDatabase visit = new VisitDatabase();

    @Override
    public void start(Stage primaryStage)
    {
        //初始化设置各个文本框
        tfSno.setPrefColumnCount(10);
        tfSno.setAlignment(Pos.BOTTOM_LEFT);
        tfName.setPrefColumnCount(10);
        tfGender.setPrefColumnCount(10);
        tfCno.setPrefColumnCount(10);
        tfPost.setPrefColumnCount(10);
        tfAge.setPrefColumnCount(10);
        //设置主面板,添加文本框和按钮
        GridPane pane = new GridPane();
        pane.setPadding(new Insets(50, 50, 50, 50));

        pane.add(new Label("学号:"), 0, 0);
        pane.add(tfSno, 1, 0);
        pane.add(new Label("姓名:"), 0, 1);
        pane.add(tfName, 1, 1);
        pane.add(new Label("性别:"), 0, 2);
        pane.add(tfGender, 1, 2);
        pane.add(new Label("年龄:"), 0, 3);
        pane.add(tfAge, 1, 3);
        pane.add(new Label("邮编:"), 0, 4);
        pane.add(tfPost, 1, 4);
        pane.add(new Label("先修课"), 0, 5);
        pane.add(tfCno, 1, 5);

        pane.add(btFind, 0, 6);
        pane.setHalignment(btFind, HPos.CENTER);
        pane.add(btClose, 1, 6);
        pane.setHalignment(btClose, HPos.CENTER);
        //按钮功能实现
        btFind.setOnAction(e ->{
            findInfo();
        });

        btClose.setOnAction(e -> {
            closeInfo();
        });

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("FindStudentFromSQL");
        primaryStage.show();
    }
    //使用数据库访问对象,查找数据
    public void findInfo()
    {
        String info = null;
        if (!tfSno.getText().equals("") && !close)
        {   try
            {   
                info = tfSno.getText();
                String[] result = visit.getInfo(info);
                if (result[0] == null)
                {
                    tfSno.setText("Sno is't exist.");
                }
                else
                {
                    tfName.setText(result[0]);
                    tfGender.setText(result[1]);
                    tfAge.setText(result[2]);
                    tfPost.setText(result[3]);
                    tfCno.setText(result[4]);
                }
            }
            catch (SQLException ex)
            {
                tfSno.setText("SQL Not found");
            }
        }
        else if (close)
        {
            tfSno.setText("connextion has close");
        }
    }
    //关闭数据库连接
    public void closeInfo()
    {
        visit.close();
        close = true;
        tfName.setText("");
        tfGender.setText("");
        tfAge.setText("");
        tfPost.setText("");
        tfCno.setText("");
    }

    public static void main(String[] args)
    {
        Application.launch(args);
    }
}
//SELECT name, gender, age, post, cno FROM student WHERE SNO=801;

猜你喜欢

转载自blog.csdn.net/cheng_cuo_tuo/article/details/80070264