Java input storage to SQL Database using prepared statement

Java input storage to SQL Database using prepared statement



How can i take input using prepared statement using jtextfield to store it in sql database? I have used jtextfield for variables but by passing this as a parameter in prepared statement gives the following error message :- The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, JTextField). Can someone make me understand what is the problem and what solution can be applied


package employeemid;
import employeemid.DatabaseConnection;
import javax.swing.*;
import com.mysql.jdbc.PreparedStatement;
import java.awt.*;
import java.awt.event.*; // importing event package for event listener
import java.sql.Connection;
import java.sql.Statement;
public class RegForm
//Creating Static variables
static JTextField name_txt ;
static JTextField fname_txt;
static JTextField mname_txt;
static JTextField lname_txt;
static JRadioButton male;
static JRadioButton female;
static JComboBox day;
static JComboBox month;
static JComboBox year;
static JTextArea add_txtArea;
static JTextField phone_txt;
static JTextField email_txt;
static JTextField pc_txt;
static JTextField a_txt;
static JCheckBox chkbox;
static JButton submit_btn;
static JTextArea output_txtArea;
//public static void main(String args)
public RegForm()

/* ---------------------------------- Creating JFrame -------------------------------------------------------- */
// 1 : Creating a frame using JFrame class
JFrame frame=new JFrame("Registration Form Example");
frame.setVisible(true);
frame.setBounds(200,100,700,600 );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 2 : setting background color of Frame.
Container c=frame.getContentPane();
c.setLayout(null);
c.setBackground(Color.cyan);
/*---------------------------------- Creating JLabel for Heading Text ------------------------------------------- */
Font f=new Font("Arial",Font.BOLD,20); // Creating font style and size for heading
// 3 : creating JLabel for Heading
JLabel heading_lbl=new JLabel();
heading_lbl.setBounds(250,5,200,40);
heading_lbl.setText("<html><font><u><b>Registration Form</b></u></html>");
// applying font on heading Label
heading_lbl.setFont(f);
/* ----------------------------------- Creating Global Font style for all components ------------------------------ */
Font f1=new Font("Arial",Font.BOLD,14);
/* ----------------------------------- Creating components for Registration details ---------------------------------- */
// 4 : Creating JLabel for Name
JLabel name_lbl=new JLabel("Name : ");
name_lbl.setBounds(50,80,100,30);
// Creating JTextField for Name
name_txt=new JTextField();
name_txt.setBounds(180,80,180,30);
// 5 : Creating JLabel for Father's Name
JLabel fname_lbl=new JLabel("Father's Name : ");
fname_lbl.setBounds(50,120,150,30);
// Creating JTextField for Father's name
fname_txt=new JTextField();
fname_txt.setBounds(180,120,180,30);
// 6 : Creating JLabel for Mother's Name
JLabel mname_lbl=new JLabel("Mother's Name : ");
mname_lbl.setBounds(50,160,150,30);
// Creating JTextField for Mother's name
mname_txt=new JTextField();
mname_txt.setBounds(180,160,180,30);
// 7 : Creating JLabel for Last Name
JLabel lname_lbl=new JLabel("Last Name : ");
lname_lbl.setBounds(50,200,150,30);
// Creating JTextField for Mother's name
lname_txt=new JTextField();
lname_txt.setBounds(180,200,180,30);
// 8 : Creating JLabel for Gender
JLabel gender_lbl=new JLabel("Gender : ");
gender_lbl.setBounds(50,240,150,30);
// Setting Cursor for components
Cursor cur=new Cursor(Cursor.HAND_CURSOR);
// Creating JRadioButton for the Male
male=new JRadioButton("Male");
male.setBounds(180,240,70,30);
male.setBackground(Color.cyan);
male.setCursor(cur);
// Creating JRadioButton for the Female
female=new JRadioButton("Female");
female.setBounds(280,240,80,30);
female.setBackground(Color.cyan);
female.setCursor(cur);
// Creating ButtonGroup for the JRadioButtons
ButtonGroup gender_grp=new ButtonGroup();
gender_grp.add(male); // adding male radio button in the ButtonGroup
gender_grp.add(female); // adding female radio button in the ButtonGroup
// 9 : Creating JLabel for Date of Birth
JLabel dob_lbl=new JLabel("Date of Birth : ");
dob_lbl.setBounds(50,280,100,30);
// Creating JComboBox for the day
String day_arr=new String[31];
for(int i=1;i<=31;i++)
day_arr[i-1]=Integer.toString(i);
day=new JComboBox(day_arr);
day.setBounds(180,280,40,30);
// Creating JComboBox for the month
String month_arr="Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","Nov","Dec" ;
month=new JComboBox(month_arr);
month.setBounds(230,280,60,30);
// Creating JComboBox for the year
String year_arr=new String[70];
for(int i=1951;i<=2020;i++)
year_arr[i-1951]=Integer.toString(i);
year=new JComboBox(year_arr);
year.setBounds(300,280,60,30);
// 10 : Creating JLabel for the Address
JLabel add_lbl=new JLabel("Address : ");
add_lbl.setBounds(50,320,100,30);
// Creating JTextArea for the address
add_txtArea= new JTextArea();
add_txtArea.setBounds(180,320,180,100);
// 11 : Creating JLabel for the phone
JLabel phone_lbl=new JLabel("Phone No. : ");
phone_lbl.setBounds(50,450,100,30);
// Creating JTextField for the phone
phone_txt=new JTextField();
phone_txt.setBounds(180,450,180,30);
// 12 : Creating JLabel for the Email
JLabel email_lbl=new JLabel("Email : ");
email_lbl.setBounds(50,490,100,30);
// Creating JTextField for the Email
email_txt=new JTextField();
email_txt.setBounds(180,490,180,30);
// 13 : Creating JLabel for the Pan Card
JLabel pc_lbl=new JLabel("PanCard no: ");
pc_lbl.setBounds(50,530,100,30);
// Creating JTextField for the pan card
pc_txt=new JTextField();
pc_txt.setBounds(180,530,180,30);
// 14 : Creating JLabel for the Aadhaar
JLabel a_lbl=new JLabel("Aadhar no : ");
a_lbl.setBounds(50,570,100,30);
// Creating JTextField for the Aadhar
a_txt=new JTextField();
a_txt.setBounds(180,570,180,30);
// 15 : Creating JCheckBox for the license agreement
chkbox=new JCheckBox("I accept the terms and conditions");
chkbox.setBounds(50,610,300,30);
chkbox.setBackground(Color.cyan);
// 16 : Creating JButton for submit the details
submit_btn=new JButton("Submit");
submit_btn.setBounds(180,680,120,40);
submit_btn.setCursor(cur); // Applying hand cursor on the button
// 17 : Adding ActionListener on submit button
submit_btn.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent event)
submit_action(event);

);
// 18 : Creating JTextArea for output
output_txtArea=new JTextArea();
output_txtArea.setBounds(380,80,500,320);
// 19 : Applying Global Font on all the JLabels
name_lbl.setFont(f1);
fname_lbl.setFont(f1);
mname_lbl.setFont(f1);
lname_lbl.setFont(f1);
gender_lbl.setFont(f1);
dob_lbl.setFont(f1);
add_lbl.setFont(f1);
phone_lbl.setFont(f1);
email_lbl.setFont(f1);
pc_lbl.setFont(f1);
a_lbl.setFont(f1);
// 20 : Applying Font on all JTextFields, JRadioButtons, JComboBox and JTextArea
name_txt.setFont(f1);
fname_txt.setFont(f1);
mname_txt.setFont(f1);
lname_txt.setFont(f1);
male.setFont(f1);
female.setFont(f1);
add_txtArea.setFont(f1);
phone_txt.setFont(f1);
email_txt.setFont(f1);
pc_txt.setFont(f1);
a_txt.setFont(f1);
chkbox.setFont(f1);
submit_btn.setFont(f1);
output_txtArea.setFont(f1);
// 21 : Adding label components to the container
c.add(heading_lbl);
c.add(name_lbl);
c.add(fname_lbl);
c.add(mname_lbl);
c.add(lname_lbl);
c.add(gender_lbl);
c.add(male);
c.add(female);
c.add(dob_lbl);
c.add(add_lbl);
c.add(phone_lbl);
c.add(email_lbl);
c.add(pc_lbl);
c.add(a_lbl);
// 22 : Adding JTextField, JTextArea, JComboBox, JCheckBox, JRadioButton to the container
c.add(name_txt);
c.add(name_txt);
c.add(fname_txt);
c.add(mname_txt);
c.add(lname_txt);
c.add(day);
c.add(month);
c.add(year);
c.add(add_txtArea);
c.add(phone_txt);
c.add(email_txt);
c.add(pc_txt);
c.add(a_txt);
c.add(chkbox);
c.add(submit_btn);
c.add(output_txtArea);

// 23 : Reading value from the Registration Form
public static void submit_action(java.awt.event.ActionEvent event)
if(chkbox.isSelected()==true)

String name=name_txt.getText();
String fname=fname_txt.getText();
String mname=mname_txt.getText();
String lname=lname_txt.getText();
String gender="Male";
if(female.isSelected()==true)
gender="Female";
String day_name=(String)day.getSelectedItem();
String month_name=(String)month.getSelectedItem();
String year_name=(String)year.getSelectedItem();
String add=add_txtArea.getText();
String phone=phone_txt.getText();
String email=email_txt.getText();
String pc=pc_txt.getText();
String a=a_txt.getText();
//PreparedStatement ps=conn.prepareStatement
//String query = "insert into data(Name,MiddleName,LastName,gender,Bday,Bmonth,Byear,Address,PhoneNo,Mail,PAN,Addhar) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try
Connection conn_1 = DatabaseConnection.getConnection();
//Statement stmt = conn_1.createStatement();
//first you "prepare" your statement (where the '?' acts as a kind of placeholder)
PreparedStatement st = (PreparedStatement) conn_1.prepareStatement( "insert into data (Name,MiddleName,LastName,gender,Bday,Bmonth,Byear,Address,PhoneNo,Mail,PAN,Addhar) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
//String query =
//now you bind the data to your parameters
st.setString(1, name_txt);//error from here
st.setString(2, fname_txt);
st.setString(3, mname_txt);
st.setString(4, lname_txt);
st.setString(5, gender);
st.setString(6, day);
st.setString(7, month);
st.setString(8, year);
st.setString(9, add_txtArea);
st.setString(10, phone_txt);
st.setString(11, email_txt);
st.setString(12, pc_txt);
st.setString(13, a_txt);
//and then you can execute it
st.executeUpdate();
catch (Exception e)
System.out.println(e);

// displaying value in the JTextArea
output_txtArea.setText(" Name : " +name + "n Father's Name : " +fname +"nMother's Name: "+mname+"nLastName: "+lname+ "n Gender : "+gender +
"n Date of Birth : "+day_name + " "+month_name + " " +year_name +
"n Address : "+add + " n Phone no : "+phone +
"n Email : "+email + "nPan Card no: "+pc+ "nAadhaar no"+a+ "n ");

else

output_txtArea.setText("Please accept the terms and condition");


/*public void reset()
for(JTextField field : fieldList)
field.setText("");

*/




1 Answer
1



You need to change


st.setString(1, name_txt);
st.setString(2, fname_txt);
st.setString(3, mname_txt);
st.setString(4, lname_txt);
...
st.setString(9, add_txtArea);
st.setString(10, phone_txt);
st.setString(11, email_txt);
st.setString(12, pc_txt);
st.setString(13, a_txt);



to


st.setString(1, name);
st.setString(2, fname);
st.setString(3, mname);
st.setString(4, lname);
...
st.setString(9, add);
st.setString(10, phone);
st.setString(11, email);
st.setString(12, pc);
st.setString(13, a);



Becuase you assigned these strings to variables:


String name=name_txt.getText();
String fname=fname_txt.getText();
String mname=mname_txt.getText();
String lname=lname_txt.getText();
...
String add=add_txtArea.getText();
String phone=phone_txt.getText();
String email=email_txt.getText();
String pc=pc_txt.getText();
String a=a_txt.getText();





thanks I tried using that first before switching to .gettext() It takes input but but storing values is where the error occurs
– Vishnu Ezhuthachan
Aug 21 at 15:41






I have no idea what you just said
– ryvantage
Aug 21 at 17:19






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

ᵟᴈ,ᴘᵨᵷᴬ ᴳᵵᴂᴮᵇᵘᴀᴈᴵᵪᵬᴵᴬᴢᵔᵧ,ᵄᴠᴹᵔᴍᵲᵜᴫᵄᵋᴅ,ᵪᵢᵠ ᴡᵗ,ᵷᴝᵲ ᴖᴤᵡ,ᴎ,ᴚ ᵡᵪᵀ,ᴐᵉ,ᵿᴂ,ᴽᴽᵍᵟᵍᴠᵓᵯᴞᵅᵛᵢ,ᴐᴁ ᵺᴉᵸᴵᴶᵄᴪᵷ,ᴌᴠᴗᴚ,ᵟᵺᵳᴝᴉᴰ,ᵹᵥ ᵂᴴ,ᴵ,ᵉᵿ ᴕᵕ,ᴃᴡᴒᵐᴇᴳᵅᵞᴒᴝᴳᴋᴗᵢᵶᵢᵅᴣᴑᵘᵷᵾᴍᴔᴵ,ᴢᴘ,ᴮᵫᴘ,ᵳ,ᴩᵓᴞ