Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to insert date values to database using java swings

 
Narendra kodli
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

can any buddy tell me how to insert date values from a calender to data base by using java swings
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To start with, SwingIsAProperNoun !
Secondly, swing is a front end technology framework. It has nothing which will let you interact with the DB directly. You will need to write JDBC code to do that.
 
Rob Camick
Ranch Hand
Posts: 2583
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out JDBC Database Access for the basics of usingSQL with Java.
 
Narendra kodli
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply. Ya i know how to link database but my problem is to create a form with cambobox, pressing on which a calender should be displayed and after selecting date month year it should be set to textfield so that i can insert it into the database. is their any code to do that
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narendra kodli wrote:Thank you for the reply. Ya i know how to link database but my problem is to create a form with cambobox, pressing on which a calender should be displayed and after selecting date month year it should be set to textfield so that i can insert it into the database. is their any code to do that

No. You will have to code it yourself.
All these components will provide you with appropriate getterXXX methods which you can use to retrieve user selected/input values. Once you have these values you can put them in your DB.
For a nice calendar component, check out JCalendar
 
Narendra kodli
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok... Now i am able to get date values and my new task is to calculate to date from from date. That is if i give todays date it should calculate and display next year date. I am using spinnerdatemodel and jspinner. How to do that
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> That is if i give todays date it should calculate and display next year date. I am using spinnerdatemodel and jspinner. How to do that

post the code you've tried.
 
Narendra kodli
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Here is my code


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.text.*;
import javax.swing.event.*;
public class calender extends JFrame {


public calender(){

final JFormattedTextField t1=new JFormattedTextField();
final JFormattedTextField t2=new JFormattedTextField();


SpinnerDateModel model = new SpinnerDateModel(new Date(), null, null,
Calendar.DAY_OF_YEAR);

final JSpinner spinner = new JSpinner(model);
spinner.setPreferredSize(new Dimension(120, spinner.getPreferredSize().height));
JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "MMM dd yyyy");
editor.getTextField().setHorizontalAlignment(JTextField.CENTER);

final SpinnerDateModel model1 = new SpinnerDateModel(new Date(), null, null,
Calendar.DAY_OF_YEAR);
final JSpinner spinner1 = new JSpinner(model1);
spinner1.setPreferredSize(new Dimension(120, spinner1.getPreferredSize().height));
JSpinner.DateEditor editor1 = new JSpinner.DateEditor(spinner1, "MMM dd yyyy");
editor1.getTextField().setHorizontalAlignment(JTextField.CENTER);

spinner1.setEditor(editor1);
spinner.setEditor(editor);

spinner.addChangeListener(new ChangeListener()
{

public void stateChanged(ChangeEvent e)
{
t1.setValue(spinner.getValue());

}
});

spinner1.addChangeListener(new ChangeListener()
{

public void stateChanged(ChangeEvent e1)
{

t2.setValue(spinner1.getValue());
}
});


final JFrame frame=new JFrame("Date Entry");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JPanel panel=new JPanel();
panel.setLayout(null);

panel.setBackground(Color.cyan);
frame.add(panel);

final JButton ok =new JButton("OK");
final JButton close =new JButton("Close");

ok.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent ae){
if (ae.getSource()==ok) {


}
}});

close.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent ae){
if (ae.getSource()==close) {

frame.setVisible(false);
}
}});

Label l1=new Label("Period Selection:");
Label l2=new Label("From Date:");
Label l3=new Label("To Date:");
Choice comb1=new Choice();
Choice comb2=new Choice();


panel.add(spinner);
panel.add(spinner1);
panel.add(l1);
panel.add(l2);
panel.add(l3);

panel.add(ok);
panel.add(close);

panel.add(comb1);
// panel.add(comb2);

panel.add(t1);
panel.add(t2);

comb1.add("MONTHLY");
comb1.add("QUARTERLY");
comb1.add("HALFYEARLY");
comb1.add("YEARLY");

comb1.setBounds(150, 55, 100, 30);
l1.setBounds(50, 50, 100, 30);

spinner.setBounds(150, 100, 100, 30);
spinner1.setBounds(150, 150, 100, 30);

l2.setBounds(81, 100, 100, 30);
l3.setBounds(95, 150, 100, 30);
//t1.setBounds(50, 200, 100, 30);
//t2.setBounds(200, 200, 100, 30);
ok.setBounds(150, 200, 80, 30);
close.setBounds(250, 200, 80, 30);
//frame.setLayout(new FlowLayout());
frame.setSize(400,300);
frame.setLocation(350,150);
frame.setVisible(true);
}


public static void main(String[] args) {
calender c=new calender();
}
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whay are you mixing awt's Label and Choice with Swing components?
why set preferred sizes, then a null layout with setBounds()?

I'd suggest you fix the GUI part before attempting to add functionality to it.
 
Narendra kodli
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok... But the problem is with next date calculation. Can you give any solution for that.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:I'd suggest you fix the GUI part before attempting to add functionality to it.
 
Narendra kodli
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why you guys stick d with gui... any ways as you said i have replaced JLables and Choice with JCombobox. now can you tell me how to find next date from given date??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I'm reading the code right, spinner1's value is to be set by a combination of spinner's date + the combobox time period.

if so:
1) create a method to set spinner1's date via java.util.Calendar class (let's call it 'cal')
set cal's time to that of spinner
create an int[] representing the periods in months 1,3,6,12 (monthly, quarterly etc)
add to cal (Calendar.MONTH field), int[comb1.getSelectedIndex()], which will add 1,3,6 or 12 months to cal.
now set spinner1's value to cal.
2) add listeners to spinner (not spinner1) and to comb1, so that whenever spinner or comb1 change,
the change will reset spinner1's value
 
Aromal ayyappan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

By overriding the getNextValue() method , I think you can achieve the incremening the year by one.

SpinnerDateModel model = new SpinnerDateModel(new Date(), null, null, Calendar.YEAR){

public Object getNextValue() {

Calendar cal = Calendar.getInstance();
cal.setTime((Date)getValue());
cal.add(Calendar.YEAR,1);
//cal.add(getCalendarField(), 1);
Date next = cal.getTime();
return next;
}
};

 
Narendra kodli
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By above method i am able to change only day or only month or only year for only one time. But i want all three fields to be changed based on spinner change listener for each change made.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narendra, please UseOneThreadPerQuestion and BeForthrightWhenCrossPostingToOtherSites.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic