JAVAFX 上传商品照片到数据库并显示出来

①   首先看效果图

点击上传商品

点击上传照片

点击所要上传的商品照片

点击添加后,数据库中会得到图片路径 

 ②    上代码

 一些窗口label和textbox布局还有按钮功能设置

package Second;

import javafx.event.ActionEvent;
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.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import sample.character.goods;
import sample.dao.GoodsDao;
import sample.util.DbUtil;
import sample.util.StringUtil;
import javax.swing.*;
import java.io.File;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

public class GoodsAddForm implements EventHandler<ActionEvent> {
    DbUtil dbUtil=new DbUtil();
    private String uNo;
    private String identity;
    private String gName;
    public GoodsAddForm(String identity,String uNo,String gName) {
        this.identity = identity;
        this.uNo=uNo;
        this.gName=gName;
    }

    GoodsDao goodsDao=new GoodsDao();
    final TextField gN=new TextField();
    final TextField gP=new TextField();
    final TextField gK=new TextField();
    final TextField LJ=new TextField();
    final Label labelgN=new Label("商品名称:");
    final Label labelgP=new Label("商品价格:");
    final Label labelgK=new Label("商品类型:");
    final Label P_Lj=new Label("图片路径:");
    final Label labelsC=new Label("上传商品照片:");
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    public void handle(ActionEvent event) {
      /*新建一个网格面板
      * */
        gN.setPromptText("输入商品名称");
        gP.setPromptText("输入商品价格(元)");
        gK.setPromptText("输入商品类型");
        LJ.setPromptText("图片路径");


        Stage stage = new Stage();
        GridPane rootGP=new GridPane();
        rootGP.setAlignment(Pos.CENTER);
        rootGP.setHgap(10);
        rootGP.setVgap(10);
        rootGP.setPadding(new Insets(10,10,10,10));

        rootGP.add(labelgN,0,0);
        rootGP.add(labelgP,0,2);
        rootGP.add(labelgK,0,4);
        rootGP.add(gN,1,0);
        rootGP.add(gP,1,2);
        rootGP.add(gK,1,4);
        rootGP.add(LJ,1,6);
        rootGP.add(P_Lj,0,6);
        rootGP.add(labelsC,0,7);

        final Button TJ=new Button("添加");
        final Button CZ=new Button("重置");
        final Button sC=new Button("上传照片");

        /*重置操作*/
        CZ.setOnAction(e -> {
            restA();

        });

        /*关闭窗口*/
        TJ.setOnAction(e -> {
            GoodAdd();
            stage.close();
            System.out.println(df.format(new Date()));
        });

//中间代码为下面图片上传功能

       :
       :
       :
   rootGP.add(TJ,0,9);
        rootGP.add(CZ,1,9);
        rootGP.add(sC,1,7);


        /*创建一个面板*/
        BorderPane rootPane = new BorderPane();
        rootPane.setCenter(rootGP);
        Scene scene = new Scene(rootPane, 500, 450);
        stage.setScene(scene);
        stage.setResizable(false);//不可修改界面大小

        rootGP.setStyle("-fx-background-color: cornsilk; ");
        stage.setTitle("上传商品");
        stage.getIcons().add(new Image("/sample/img/11.jpg"));
        stage.setScene(scene);
        stage.centerOnScreen();
        stage.show();

    }

 /*重置方法*/
        private void restA() {
            Clear();
        }
        private void Clear()
          {
                this.gN.setText("");
                this.gK.setText("");
                this.gP.setText("");
                this.LJ.setText("");
                }




 主要的上传图片功能

  /*
         *
         * 图片上传功能,点击sC按钮执行
         *
         * */
        sC.setOnAction(new EventHandler<ActionEvent>() {
                           @Override
                           /*上传本地*/
                           public void handle(ActionEvent event) {
                               Stage stage=new Stage();
                               FileChooser fc=new FileChooser();
                               final String[] a = {new String()};

                               fc.setTitle("上传图片");
                               fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("图片类型","*.jpg","*.png","*.jpeg"));
                               fc.setInitialDirectory(new File("D:\\Seconderary\\src\\sample\\img"));   //文件目录
                               List<File> list=fc.showOpenMultipleDialog(stage);
                               list.forEach(new Consumer<File>() {
                                   @Override
                                   public void accept(File file) {
                                       a[0] = file.getAbsolutePath();
                                       System.out.println(a[0]);
                                       final ImageView imageView = new ImageView(
                                               new Image("file:\\"+a[0],200,200,false,false)
                                       );
                                       System.out.println("file:\\"+a[0]);
                                       final Button tp=new Button("",imageView);
                                       rootGP.add(tp,2,7);
                                   }
                               });
                               LJ.setText(a[0]);
                               //fc.showOpenDialog(stage);

                           }
                       }
        );

 数据库的连接问题

  private  void GoodAdd() {
        String gN1 = this.gN.getText();
        String gP1 = this.gP.getText();
        String gk1 = this.gK.getText();
        String xh1=this.uNo;
        String LJ1= this.LJ.getText();
        String date= this.df.toString();

        //判断是否为空
        if (StringUtil.isEmpty(gN1)) {
            JOptionPane.showMessageDialog(null, "商品名称不能为空!");
        }
        if (StringUtil.isEmpty(gP1)) {
            JOptionPane.showMessageDialog(null, "商品价格不能为空!");
        }

        goods good=new goods(gN1, gP1 ,gk1 ,xh1,LJ1,date);
        Connection con=null;
            try {
                con=dbUtil.getCon();
                int n= 0;
                n = goodsDao.add(con,good);
                if (n==1){ JOptionPane.showMessageDialog(null, "商品上传成功!");}


                else{
                    JOptionPane.showMessageDialog(null, "商品上传失败!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                try{
                    dbUtil.closeCon(con);//关闭数据库
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

(数据库要自己建,字段问题要与数据库一致,否则连接错误)

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在JavaFX中连接到SQL Server数据库,您需要执行以下步骤: 1. 下载并安装JDBC驱动程序:您需要下载适用于SQL Server的JDBC驱动程序。您可以从Microsoft官方网站上下载JDBC驱动程序。 2. 添加JDBC驱动程序到类路径:将下载的JDBC驱动程序添加到JavaFX项目的类路径中。 3. 导入必要的类:导入JavaFX和JDBC所需的类。 4. 建立连接:使用JDBC驱动程序建立与SQL Server数据库的连接。 以下是一个简单的JavaFX代码示例,用于连接到SQL Server数据库: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import javafx.application.Application; import javafx.stage.Stage; public class SQLServerConnection extends Application { // JDBC驱动程序和数据库URL static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; static final String DB_URL = "jdbc:sqlserver://localhost;databaseName=YourDBName"; // 数据库用户名和密码 static final String USER = "yourusername"; static final String PASS = "yourpassword"; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { Connection conn = null; try { // 注册JDBC驱动程序 Class.forName(JDBC_DRIVER); // 建立连接 System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // 连接成功 System.out.println("Connected to database successfully!"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 您需要将上面的代码中的“YourDBName”替换为您的SQL Server数据库名称,“yourusername”和“yourpassword”替换为您的数据库用户名和密码。 这是一个基本的示例代码,您可以根据需要修改和扩展它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值