Tutorial on Java GUI and MySQL

Sending data from gui to database

Page 1 of 1

12 Replies - 28579 Views - Last Post: 11 December 2011 - 03:20 PM Rate Topic: -----

#1 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Tutorial on Java GUI and MySQL

Posted 02 January 2009 - 12:57 PM

Hello,
I'm creating my first Java GUI. It's very basic and contains several Text Fields and a "Submit" Button. I'm looking for a tutorial that will show me how to take all the user entered text/data from the GUI and store it in Mysql when the user clicks on the submit button.
Thanks in Advance
Is This A Good Question/Topic? 0
  • +

Replies To: Tutorial on Java GUI and MySQL

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Tutorial on Java GUI and MySQL

Posted 02 January 2009 - 01:02 PM

The sun website has a good tutorial. Just google it. If you dont know the correct word, its JDBC, java database connectivity. So google somthing like, simple java gui, jdbc examples.
Was This Post Helpful? 1
  • +
  • -

#3 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Re: Tutorial on Java GUI and MySQL

Posted 06 January 2009 - 09:44 AM

Hello,
First of all thanks for the help. I downloaded the mysql/j connector and I'm able to send data from java to mysql. However i can only insert rows into the db if it is hard coded from my java program. When i click the "ok" button in my gui no data is sent to the database. Here's my code.
import javax.swing.*;

import com.mysql.jdbc.PreparedStatement;

import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.EventObject;

public class buttoninput extends JFrame
						implements ActionListener	
{		
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Label	instruction;
	private Label	company;
	private TextField companyfield;
	private Button  okButton;
	private Button	clearformButton;
	private Label	Name;
	private TextField namefield;
	private Label	Address;
	private TextField addressfield;
	private Label	City;
	private TextField cityfield;
	private Label	State;
	private TextField statefield;
	private Label   Zipcode;
	private TextField zipcodefield;
	private Label	Phone;
	private TextField phonefield;
	private Label	Fax;
	private TextField faxfield;
	private Label	Email;
	private TextField emailfield;
	private MenuBar	menubar;
	private Menu	fileMenu, editMenu, helpMenu;
	private MenuItem openMI, uploadMI, exitMI;
	private MenuItem deleteMI;
	private MenuItem helpMI;
	private Object msgLine;
	
	buttoninput()
	{
		super();
		setTitle("Mailing List");
		setSize(400,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(200,100);
		setBackground(java.awt.Color.lightGray);
		
				
		instruction = new Label("Enter customer information");
		company 	= new Label("Company");
		companyfield= new TextField(5);
		okButton	= new Button("Ok");
		Name		= new Label("Name");
		namefield   = new TextField(5);
		Address		= new Label("Address");
		addressfield = new TextField(5);
		City		= new Label("City");
		cityfield   = new TextField(5);
		State		= new Label("State");
		statefield  = new TextField(5); 
		Zipcode		= new Label("Zipcode");
		zipcodefield = new TextField(5);
		Phone		= new Label("Phone");
		phonefield  = new TextField(5);
		Fax			= new Label("Fax");
		faxfield	= new TextField(5);
		Email		= new Label("Email");
		emailfield  = new TextField(5);
		clearformButton = new Button("Clear Form");
		menubar		= new MenuBar();
		fileMenu	= new Menu("File");
		editMenu	= new Menu("Edit");
		helpMenu	= new Menu("Help");
		menubar.add(fileMenu);
		menubar.add(editMenu);
		menubar.add(helpMenu);
		
		openMI		= new MenuItem("Open");
		uploadMI	= new MenuItem("Upload");
		exitMI		= new MenuItem("Exit");
		deleteMI	= new MenuItem("Delete");
		helpMI		= new MenuItem("Help");
		
		fileMenu.add(openMI);
		fileMenu.add(uploadMI);
		fileMenu.add(exitMI);
		editMenu.add(deleteMI);
		helpMenu.add(helpMI);
		
		
		setLayout(new GridLayout(16,16));
		setMenuBar(menubar);
		setVisible(true);
		add(instruction);
		add(new Label(" "));
		add(new Label(" "));
		add(new Label(""));
		add(company);
		add(companyfield);
		add(new Label("Name"));
		add(namefield);
		add(new Label("Address"));
		add(addressfield);
		add(new Label("City"));
		add(cityfield);
		add(new Label("State"));
		add(statefield);
		add(new Label("Zipcode"));
		add(zipcodefield);
		add(new Label("Phone"));
		add(phonefield);
		add(new Label("Fax"));
		add(faxfield);
		add(new Label("Email"));
		add(emailfield);
		add(new Label(""));
		add(new Label(""));
		add(new Label(""));
		add(okButton);
		add(new Label(""));
		add(clearformButton);
		pack();
		setVisible(true);
		okButton.addActionListener(this);}
		
		
	public void actionPerformed1(ActionEvent evt) 
	{
		Object part = evt.getSource();
		if ((part == okButton))
			okButton.setEnabled(true);
		{
		try {String userName = "xxxx"; 
		String password = "xxxxx"; 
		String url = "jdbc:mysql://localhost:3306/xxxxxxx";
		Connection conn = DriverManager.getConnection (url, userName, password); 
		Statement st = conn.createStatement();
		
		String company=companyfield.getText();
		String Name=namefield.getText();
		String Address=addressfield.getText();
		String City=cityfield.getText();
		String State=statefield.getText();
		String Zipcode=zipcodefield.getText();
		String Phone=phonefield.getText();
		String Fax=faxfield.getText();
		String Email=emailfield.getText();

		String insertStr="";
		insertStr="insert into customers (company, owner, address, city, state, zipcode, phone, fax, email) values("
			+quotate(company)+","
			+quotate(Name)+","
			+quotate(Address)+","
			+quotate(City)+","
			+quotate(State)+","
			+quotate(Zipcode)+","
			+quotate(Phone)+","
			+quotate(Fax)+","
			+quotate(Email)+","
			+")";

								

		
	} catch (Exception e) {
		System.err.println("Got an exception! ");
		System.err.println(e.getMessage());
	}}
	
}
		
		private String quotate(String company2) {
		// TODO Auto-generated method stub
		return null;
	}
		private String quotate(TextField companyfield2) {
		// TODO Auto-generated method stub
		return null;
	}
		public static void main(String args[])
	{
		buttoninput test = new buttoninput();
	}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
		}


Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Tutorial on Java GUI and MySQL

Posted 06 January 2009 - 02:05 PM

Not sure what you are trying to do. When you set your String, you are passing your variables to a method which returns null. Makes no sense. Also, you never execute your statement. If i was u, i would look at doing a Prepared Statement, think its a lot more professional. You could try somthing simular to this
PreparedStatement ps = null;
try{
String userName = "xxxx"; 
String password = "xxxxx"; 
String url = "jdbc:mysql://localhost:3306/xxxxxxx";
Connection conn = DriverManager.getConnection (url, userName, password); 

String cmd =insert into customers (company, owner, address, city, state, zipcode, phone, fax, email) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
ps = conn.prepareStatement(cmd);
ps.setString(1, Company);  
ps.setString(2, Name); 
ps.setString(3, Address);  
ps.setString(4, City);
//same for rest  

ps.executeUpdate(); //execute your statement
} catch (Exception e) {
e.printStackTrace();
}
finally{
ps.close();
}


Somthing simular to the above. And you might want to reconsider you if statement when it enters the action event.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tutorial on Java GUI and MySQL

Posted 06 January 2009 - 10:07 PM

You never execute the insertStr statement that you built
your quotate() methods return null
and even if they were returning the good values this is wrong

+quotate(Fax)+","
+quotate(Email)+","
+")";

and should be replaced by

+quotate(Fax)+","
+quotate(Email)
+";)";

You have an extra coma at the end of your SQL statement and the final ";" is missing

Better to use PreparedStatement they do not required the "" for String and the rightmost ;

Also not a good isea at all to connect to the server in the actionPerformed() method
do it once and keep the connection open in your "main" metjod
Was This Post Helpful? 0
  • +
  • -

#6 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Re: Tutorial on Java GUI and MySQL

Posted 07 January 2009 - 08:55 AM

View Postpbl, on 6 Jan, 2009 - 09:07 PM, said:

You never execute the insertStr statement that you built
your quotate() methods return null
and even if they were returning the good values this is wrong

+quotate(Fax)+","
+quotate(Email)+","
+")";

and should be replaced by

+quotate(Fax)+","
+quotate(Email)
+";)";

You have an extra coma at the end of your SQL statement and the final ";" is missing

Better to use PreparedStatement they do not required the "" for String and the rightmost ;

Also not a good isea at all to connect to the server in the actionPerformed() method
do it once and keep the connection open in your "main" metjod




Should I use this statement -"Connection conn = DriverManager.getConnection();" in the main method?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tutorial on Java GUI and MySQL

Posted 07 January 2009 - 06:18 PM

View Postcontrolj, on 7 Jan, 2009 - 07:55 AM, said:

Should I use this statement -"Connection conn = DriverManager.getConnection();" in the main method?


No because the actionPerformed() method wont be able to see it.

You should have:

Connection conn;

in the instance variables of buttoninput (where your Label and Button are)
and do
conn = DriverManager.getConnection();
in the constructor of buttoninput
Was This Post Helpful? 0
  • +
  • -

#8 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Re: Tutorial on Java GUI and MySQL

Posted 12 January 2009 - 09:30 AM

Hello,
I made a few changes to my source code.
import javax.swing.*;
import com.mysql.jdbc.StatementImpl;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class buttoninput extends JFrame
						implements ActionListener{
	

	JButton myButton = new JButton("okButton");
	JButton myButton2 = new JButton("ClearForm");
	private static final long serialVersionUID = 1L;
	private Label	instruction;
	private Label	company;
	private TextField companyfield;
	private Label	Name;
	private TextField namefield;
	private Label	Address;
	private TextField addressfield;
	private Label	City;
	private TextField cityfield;
	private Label	State;
	private TextField statefield;
	private Label   Zipcode;
	private TextField zipcodefield;
	private Label	Phone;
	private TextField phonefield;
	private Label	Fax;
	private TextField faxfield;
	private Label	Email;
	private TextField emailfield;
	private MenuBar	menubar;
	private Menu	fileMenu, editMenu, helpMenu;
	private MenuItem openMI, uploadMI, exitMI;
	private MenuItem deleteMI;
	private MenuItem helpMI;
	private Label commentLabel;
	Connection conn;
	
	
	buttoninput()
	{
		super();
		setTitle("Mailing List");
		setSize(400,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(200,100);
		setBackground(java.awt.Color.lightGray);
		
		conn = DriverManager.getConnection();
				
		instruction = new Label("Enter customer information");
		company 	= new Label("Company");
		companyfield= new TextField(5);
		Name		= new Label("Name");
		namefield   = new TextField(5);
		Address		= new Label("Address");
		addressfield = new TextField(5);
		City		= new Label("City");
		cityfield   = new TextField(5);
		State		= new Label("State");
		statefield  = new TextField(5); 
		Zipcode		= new Label("Zipcode");
		zipcodefield = new TextField(5);
		Phone		= new Label("Phone");
		phonefield  = new TextField(5);
		Fax			= new Label("Fax");
		faxfield	= new TextField(5);
		Email		= new Label("Email");
		emailfield  = new TextField(5);
		menubar		= new MenuBar();
		fileMenu	= new Menu("File");
		editMenu	= new Menu("Edit");
		helpMenu	= new Menu("Help");
		menubar.add(fileMenu);
		menubar.add(editMenu);
		menubar.add(helpMenu);
		myButton.addActionListener(this);
		
		
		openMI		= new MenuItem("Open");
		uploadMI	= new MenuItem("Upload");
		exitMI		= new MenuItem("Exit");
		deleteMI	= new MenuItem("Delete");
		helpMI		= new MenuItem("Help");
		
		fileMenu.add(openMI);
		fileMenu.add(uploadMI);
		fileMenu.add(exitMI);
		editMenu.add(deleteMI);
		helpMenu.add(helpMI);
		
		
		setLayout(new GridLayout(16,16));
		setMenuBar(menubar);
		setVisible(true);
		add(instruction);
		add(new Label(" "));
		add(new Label(" "));
		add(new Label(""));
		add(company);
		add(companyfield);
		add(new Label("Name"));
		add(namefield);
		add(new Label("Address"));
		add(addressfield);
		add(new Label("City"));
		add(cityfield);
		add(new Label("State"));
		add(statefield);
		add(new Label("Zipcode"));
		add(zipcodefield);
		add(new Label("Phone"));
		add(phonefield);
		add(new Label("Fax"));
		add(faxfield);
		add(new Label("Email"));
		add(emailfield);
		add(new Label(""));
		add(new Label(""));
		add(new Label(""));
		add(myButton);
		add(new Label(""));
		add(myButton2);
		pack();
		setVisible(true);
		
		public static void main(String[] args)
		  
		{
			buttoninput test = new buttoninput();
			
		}
		public void actionPerformed(ActionEvent e)
		  {
			Object String;
			if (e.getSource() == myButton)
				
			
			String String insertStr = "";
			
			try{
				
				insertStr="insert into customers (company, owner, address, city, state, zipcode, phone, fax, email) values("
						+quotate(companyfield)+","
						+quotate(namefield)+","
						+quotate(addressfield)+","
						+quotate(cityfield)+","
						+quotate(statefield)+","
						+quotate(zipcodefield)+","
						+quotate(phonefield)+","
						+quotate(faxfield)+","
						+quotate(emailfield)+","
						+";)";
				
				
			StatementImpl stmt;
			int done=stmt.executeUpdate(insertStr);
				
				
				
			
			}
			catch(Exception e1){
				e1.printStackTrace();
			}}
 		public String quotate(TextField namefield2){
 			
 			return "'"+namefield2+"'";
 		}
			  
			{
			 
		  }
		{}
		
		
	
	
}




Is this correct?

This post has been edited by controlj: 12 January 2009 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tutorial on Java GUI and MySQL

Posted 12 January 2009 - 05:53 PM

Still an extra "," here
				   +quotate(emailfield)+","
						+";)";


Was This Post Helpful? 0
  • +
  • -

#10 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Re: Tutorial on Java GUI and MySQL

Posted 13 January 2009 - 09:40 AM

Sorry, I posted the wrong file yesterday. The final one here has the extra ";" deleted... Eclipse is not showing any errors on my source code however, I'm still not able to insert the textfields' data into mysql when i click the "Ok" button from the GUI.
import javax.swing.*;
import com.mysql.jdbc.StatementImpl;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class buttoninput extends JFrame
						implements ActionListener{
	

	JButton myButton = new JButton("okButton");
	JButton myButton2 = new JButton("ClearForm");
	private static final long serialVersionUID = 1L;
	private Label	instruction;
	private Label	company;
	private TextField companyfield;
	private Label	Name;
	private TextField namefield;
	private Label	Address;
	private TextField addressfield;
	private Label	City;
	private TextField cityfield;
	private Label	State;
	private TextField statefield;
	private Label   Zipcode;
	private TextField zipcodefield;
	private Label	Phone;
	private TextField phonefield;
	private Label	Fax;
	private TextField faxfield;
	private Label	Email;
	private TextField emailfield;
	private MenuBar	menubar;
	private Menu	fileMenu, editMenu, helpMenu;
	private MenuItem openMI, uploadMI, exitMI;
	private MenuItem deleteMI;
	private MenuItem helpMI;
	private boolean myButtonPressed = false;
	Connection conn;
	
	
	buttoninput() throws SQLException
	{
		super();
		setTitle("Mailing List");
		setSize(400,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(200,100);
		setBackground(java.awt.Color.lightGray);
		String userName = "xxxx"; 
		String password = "xxxxxx"; 
		String url = "jdbc:mysql://localhost:3306/xxxxxxxxx";
		conn = DriverManager.getConnection(url, userName, password);
		
						
		instruction = new Label("Enter customer information");
		company 	= new Label("Company");
		companyfield= new TextField(5);
		Name		= new Label("Name");
		namefield   = new TextField(5);
		Address		= new Label("Address");
		addressfield = new TextField(5);
		City		= new Label("City");
		cityfield   = new TextField(5);
		State		= new Label("State");
		statefield  = new TextField(5); 
		Zipcode		= new Label("Zipcode");
		zipcodefield = new TextField(5);
		Phone		= new Label("Phone");
		phonefield  = new TextField(5);
		Fax			= new Label("Fax");
		faxfield	= new TextField(5);
		Email		= new Label("Email");
		emailfield  = new TextField(5);
		menubar		= new MenuBar();
		fileMenu	= new Menu("File");
		editMenu	= new Menu("Edit");
		helpMenu	= new Menu("Help");
		menubar.add(fileMenu);
		menubar.add(editMenu);
		menubar.add(helpMenu);
		JButton myButton = new JButton("okButton");
		myButton.addActionListener(this);
		
		
		openMI		= new MenuItem("Open");
		uploadMI	= new MenuItem("Upload");
		exitMI		= new MenuItem("Exit");
		deleteMI	= new MenuItem("Delete");
		helpMI		= new MenuItem("Help");
		
		fileMenu.add(openMI);
		fileMenu.add(uploadMI);
		fileMenu.add(exitMI);
		editMenu.add(deleteMI);
		helpMenu.add(helpMI);
		
		
		setLayout(new GridLayout(16,16));
		setMenuBar(menubar);
		setVisible(true);
		add(instruction);
		add(new Label(" "));
		add(new Label(" "));
		add(new Label(""));
		add(company);
		add(companyfield);
		add(new Label("Name"));
		add(namefield);
		add(new Label("Address"));
		add(addressfield);
		add(new Label("City"));
		add(cityfield);
		add(new Label("State"));
		add(statefield);
		add(new Label("Zipcode"));
		add(zipcodefield);
		add(new Label("Phone"));
		add(phonefield);
		add(new Label("Fax"));
		add(faxfield);
		add(new Label("Email"));
		add(emailfield);
		add(new Label(""));
		add(new Label(""));
		add(new Label(""));
		add(myButton);
		add(new Label(""));
		add(myButton2);
		pack();
		setVisible(true);
		}
	
		public static void main(String[] args) throws SQLException
		  
		{
			buttoninput test = new buttoninput();
			
		}
		public void myButtonactionPerformed(ActionEvent e)
		  {
			String Company=companyfield.getText();
			String Name=namefield.getText();
			String Address=addressfield.getText();
			String City=cityfield.getText();
			String State=statefield.getText();
			String Zipcode=zipcodefield.getText();
			String Phone=phonefield.getText();
			String Fax=faxfield.getText();
			String Email=emailfield.getText();
			
			
			String insertStr="";
			
			
			
			try{
				
				insertStr="insert into customers (company, owner, address, city, state, zipcode, phone, fax, email) values("
						+quotate1(companyfield)+","
						+quotate2(namefield)+","
						+quotate3(addressfield)+","
						+quotate4(cityfield)+","
						+quotate5(statefield)+","
						+quotate6(zipcodefield)+","
						+quotate7(phonefield)+","
						+quotate8(faxfield)+","
						+quotate9(emailfield)
						+";)";
				
				
			StatementImpl stmt;
			int done=executeUpdate(insertStr);
								  
			}
			catch(Exception e1){
				e1.printStackTrace();
			}}
 		private int executeUpdate(String insertStr) {
			// TODO Auto-generated method stub
			return 1;
		}

		
		public String quotate1(TextField companyfield)
		{
			return ","+companyfield+",";
		}
		public String quotate2(TextField namefield){
 			
 			return "'"+namefield+"'";
 		}
		public String quotate3(TextField addressfield)
		{
			return ","+addressfield+",";
		}
		public String quotate4(TextField cityfield)
		{
			return ","+cityfield+",";
		}
		public String quotate5(TextField statefield)
		{
			return ","+statefield+",";
		}
		public String quotate6(TextField zipcodefield)
		{
			return ","+zipcodefield+",";
		}
		public String quotate7(TextField phonefield)
		{
			return ","+phonefield+",";
		}
		public String quotate8(TextField faxfield)
		{
			return ","+faxfield+",";
		}
		public String quotate9(TextField emailfield)
		{
			return ","+emailfield+",";
		}
			  
			{
			 
		  }
		{}
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		
	
	
}

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tutorial on Java GUI and MySQL

Posted 13 January 2009 - 05:04 PM

Your OKButton use this as ActionListener

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
		}



Which, I hope you agree with me, does not do much
Was This Post Helpful? 0
  • +
  • -

#12 controlj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-December 08

Re: Tutorial on Java GUI and MySQL

Posted 14 January 2009 - 09:39 AM

Your right. I didn't see that error. I made a few more modifications and now my GUI is sending data to mysql.
Thank You for all the help.
Was This Post Helpful? 0
  • +
  • -

#13 raihanuahmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-December 11

Re: Tutorial on Java GUI and MySQL

Posted 11 December 2011 - 03:20 PM

could you please upload the Final code that has no error.........................
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1