Search code examples
javasqlitemysql-error-1064sqliteopenhelper

java.sql.SQLException: near “.”: syntax error


I wrote a program that populates a database with a list of words. Problem is, it throws up the "Exception in thread "main" java.sql.SQLException: near ".": syntax error" every time I try to run the code. This I turn to you for help.

This is the code

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

And here is the source:

public Account() {
            super("Create Account");
            initComponents();
            conn=javaconnect.ConnecrDb();
            RandomAcc();
            RandomMICR();
            RandomPIN();
        }

    public void RandomAcc(){
        Random ra=new Random();
        jTextField1.setText(""+ra.nextInt(10000+1));
    }
    public void RandomMICR(){
        Random ra=new Random();
        jTextField2.setText(""+ra.nextInt(1000+1));
    }
    public void RandomPIN(){
        Random ra=new Random();
        jTextField3.setText(""+ra.nextInt(1000+1));
    }
    public void Bal(){
        String sql="insert into Balances(Name,Acc,MICR_No,Balance) values(?,?,?,?)";
        try{
        pst=conn.prepareStatement(sql);
        pst.setString(1, jTextField5.getText());
        pst.setString(2, jTextField1.getText());
        pst.setString(3, jTextField2.getText());
        pst.setString(4, jTextField10.getText());
        pst.execute();   
    }catch(Exception e){`enter code here`
        JOptionPane.showMessageDialog(null, e);
    }
 }

String sql="insert into Account(Acc,Name,DOB,Pin,Acc_Type,Nationality,Caste,MICR_No,Gender,Mob,Address,Sec.Q,Sec.A,Balance) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    try{
        pst=conn.prepareStatement(sql);
        pst.setString(1, jTextField1.getText());
        pst.setString(2, jTextField5.getText());
        pst.setString(3,((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText());
        pst.setString(4, jTextField3.getText());
        pst.setString(5, (String) jComboBox1.getSelectedItem());
        pst.setString(6, (String) jComboBox2.getSelectedItem());
        pst.setString(7, jTextField6.getText());
        pst.setString(8, jTextField2.getText());

        jRadioButton1.setActionCommand("Male");
        jRadioButton2.setActionCommand("Female");
        pst.setString(9, buttonGroup1.getSelection().getActionCommand()); 

        pst.setString(10, jTextField7.getText());
        pst.setString(11, jTextField4.getText());
        pst.setString(12, (String) jComboBox3.getSelectedItem());
        pst.setString(13, jTextField9.getText());
        pst.setString(14, jTextField10.getText());
        pst.execute();
        JOptionPane.showMessageDialog(null, "Congrtz\n Account has been Created");
        Bal();
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}                                 

Thanks


Solution

  • String sql="insert into Account(Acc,Name,DOB,Pin,Acc_Type,Nationality,Caste,MICR_No,Gender,Mob,Address,Sec.Q,Sec.A,Balance) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    

    Are Sec.Q,Sec.A column names? It is not allowed.

    Database name restrictions