3 Replies - 24967 Views - Last Post: 04 July 2014 - 12:57 PM Rate Topic: -----

#1 RamHabans   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-July 14

integrating mysql into JavaFx Scene Builder

Posted 04 July 2014 - 12:34 PM

Hello , i am new to javafx and my goal is to basically just test if my java code works with Mysql.
What i'm planning to do is to display one data of the sakila database into a Label named greetingLabel.
And i can't seem to find where i went wrong so i'll show you my whole code.

Here is the main code for my controller class from scene builder:

package main;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

import java.sql.*;

public class MainController implements Initializable {
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement pst = null;
	
	@FXML
	private Label greetingLabel;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		conn = mysqlconnect.ConnecrDb();
	}
	
	public void showGreeting(ActionEvent event) throws SQLException {
	
		String sql = "SELECT first_name FROM actor where actor_id=1";
		rs= pst.executeQuery(sql);
		if(rs.next()) {
			String lbl1 = rs.getString("USER()");
			greetingLabel.setText(lbl1);
		}

	//	greetingLabel.setText("Halu woda");
		
	}

}



Here is my code for the mysql connector class

package main;

import java.sql.*;
public class mysqlconnect {
    Connection conn = null;

    public static Connection ConnecrDb() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/sakila","confidential","confidential");
            System.out.println("Connection success!");
            return conn;
        } catch(Exception e){
            System.out.println(e);
            return null;
        }
    }
}



Here is the code for the application class
package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class App extends Application{

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		Parent root = FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
		Scene scene = new Scene(root, 600, 400);
		
		primaryStage.setScene(scene);
		primaryStage.setTitle("Hello world!");
		primaryStage.show();
		
	}

}



Here is the code for the FXML file
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.MainController">
   <children>
      <BorderPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <bottom>
            <Button mnemonicParsing="false" onAction="#showGreeting" text="Button" BorderPane.alignment="BOTTOM_CENTER">
               <BorderPane.margin>
                  <Insets bottom="20.0" />
               </BorderPane.margin>
            </Button>
         </bottom>
         <center>
            <Label fx:id="greetingLabel" prefHeight="97.0" prefWidth="149.0" text="Click Me" BorderPane.alignment="CENTER" />
         </center>
      </BorderPane>
   </children>
</AnchorPane>





Here is the error displayed when pressing the button
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
	at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
	at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
	at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
	at javafx.event.Event.fireEvent(Unknown Source)
	at javafx.scene.Node.fireEvent(Unknown Source)
	at javafx.scene.control.Button.fire(Unknown Source)
	at com.sun.javafx.scene.control.behavior.Buttonbehavior.mouseReleased(Unknown Source)
	at com.sun.javafx.scene.control.skin.behaviorSkinBase$1.handle(Unknown Source)
	at com.sun.javafx.scene.control.skin.behaviorSkinBase$1.handle(Unknown Source)
	at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
	at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
	at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
	at javafx.event.Event.fireEvent(Unknown Source)
	at javafx.scene.Scene$MouseHandler.process(Unknown Source)
	at javafx.scene.Scene$MouseHandler.access$1800(Unknown Source)
	at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
	at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
	at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
	at com.sun.glass.ui.View.notifyMouse(Unknown Source)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
	at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.reflect.misc.Trampoline.invoke(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
	... 48 more
Caused by: java.lang.NullPointerException
	at main.MainController.showGreeting(MainController.java:30)
	... 57 more




Is This A Good Question/Topic? 0
  • +

Replies To: integrating mysql into JavaFx Scene Builder

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3682
  • View blog
  • Posts: 16,900
  • Joined: 20-September 08

Re: integrating mysql into JavaFx Scene Builder

Posted 04 July 2014 - 12:44 PM

Quote

rs= pst.executeQuery(sql);


You didn't create pst
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is online

  • a lovely bit of linq
  • member icon

Reputation: 6890
  • View blog
  • Posts: 28,505
  • Joined: 12-December 12

Re: integrating mysql into JavaFx Scene Builder

Posted 04 July 2014 - 12:49 PM

The error occurs on this line:
rs= pst.executeQuery(sql);

pst is still null. I believe you have to use prepareStatement() because the docs for executeQuery says:

Quote

Note: This method cannot be called on a PreparedStatement or CallableStatement.

This post has been edited by andrewsw: 04 July 2014 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 RamHabans   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-July 14

Re: integrating mysql into JavaFx Scene Builder

Posted 04 July 2014 - 12:57 PM

I have a typo in my first post, it's
String lbl1 = rs.getString("first_name");

not
String lbl1 = rs.getString("USER()");


Regarding pst, i will try to research on how preparestatement works, brb.

Ok thanks now it works.
	public void showGreeting(ActionEvent event) throws SQLException {
	
		String sql = "SELECT first_name FROM actor where actor_id=1";
	    pst=conn.prepareStatement(sql);
	    rs= pst.executeQuery();
		if(rs.next()) {
			String lbl1 = rs.getString("first_name");
			greetingLabel.setText(lbl1);
		}

	//	greetingLabel.setText("Halu woda");
		
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1