Problem while insert to data base

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pranayf
    New Member
    • May 2014
    • 2

    Problem while insert to data base

    plz resolve the issue its not able to inset to database
    hereis my code classname addc
    Code:
    package Log;
    
    
    
    
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    
     public class addc extends JFrame implements ActionListener{
    	
    	
    	public static void main(String[]args){
    		addc panel = new addc();
    	panel.setSize(600,400);
    	panel.setVisible(true);
    	panel.setResizable(false);
    	panel.setLocation(400,250);
    	}
    	
    	
    	Font f1 = new Font("", Font.BOLD, 10);
    	
    	JLabel addclient = new JLabel("Add Client");
    	
    	JLabel lblFName = new JLabel("First Name ");	
    	JLabel lblLName = new JLabel("Last Name ");
    	
    	JLabel lblAddress = new JLabel("Address");
    	JLabel lblEmail = new JLabel("Email-id");
    	JLabel lblPhone = new JLabel("Phone-No.");
    	
    	
    	JTextField txtFName= new JTextField(20);
    	JTextField txtLName = new JTextField(20);
    	
    	JTextField txtAddress = new JTextField(20);
    	JTextField txtEmail = new JTextField(20);
    	JTextField txtPhone = new JTextField(20);
    	
    	
    	JButton btnCret = new JButton(new ImageIcon("reg.png"));
    	
    	
    	
    	Connection cn;
    	Statement st;
    	PreparedStatement ps;
    	ResultSet rs;
    	PreparedStatement ps1;
    
    	
    	public addc() {
    		super("Project");
    	
    		JPanel pane = new JPanel();
    		pane.setLayout(null);
    		
    	
    		
    		lblFName.setBounds(105,85,120,25);
    		pane.add(lblFName);
    		txtFName.setBounds(225,85,150,25);
    		pane.add(txtFName);
    		
    		lblLName.setBounds(105,120,120,25); 
    		pane.add(lblLName);
    		txtLName.setBounds(225,120,150,25); 
    		pane.add(txtLName);
    		
    		lblAddress.setBounds(105,155,120,25);
    		pane.add(lblAddress);
    		txtAddress.setBounds(225,155,150,25);
    		pane.add(txtAddress);
    		
    		lblEmail.setBounds(105,190,120,25);
    		pane.add(lblEmail);
    		txtEmail.setBounds(225,190,150,25); 
    		pane.add(txtEmail);	
    		
    		lblPhone.setBounds(105,225,120,25);
    		pane.add(lblPhone);	
    		txtPhone.setBounds(225,225,150,25);
    		pane.add(txtPhone);	
    		
    		addclient.setBounds(200,5,250,60);
    		addclient.setFont(new Font("Serif", Font.BOLD, 24));
    		pane.add(addclient);
    		
    		lblFName.setForeground(Color.white);
    		lblLName.setForeground(Color.white);
    		lblAddress.setForeground(Color.white);
    		lblPhone.setForeground(Color.white);
    		lblEmail.setForeground(Color.white);
    		
    			
    		btnCret.setBounds(129,280,130,38);
    		pane.add(btnCret);
    		btnCret.addActionListener(this);
    	
    		addclient.setForeground(Color.white);
    	
    		JLabel lbl = new JLabel(new ImageIcon("background1.jpg"));
    		
    		lbl.setBounds(0,0,600,400);
    		pane.add(lbl);
    		
    		setContentPane(pane);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		
    		try{
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    			cn = DriverManager.getConnection("jdbc:odbc:project");
    			JOptionPane.showMessageDialog(null,"Successfully Connected to Database","Confirmation", JOptionPane.INFORMATION_MESSAGE);  
    		      
    		}catch(ClassNotFoundException e)  {
     			System.err.println("Failed to load driver");
     			e.printStackTrace();
     		}
     		catch(SQLException e){
     			System.err.println("Unable to connect");
     			e.printStackTrace();
     		}
    	}
    
    			public void actionPerformed(ActionEvent e){
    
    				Object source = e.getSource();
    				if(source == btnCret){
    					String suser = 	txtFName.getText();
    					String slname =	txtLName.getText();
    					String saddress =txtAddress.getText();
    					String semail = txtEmail.getText();
    					String sphone = txtPhone.getText();
    					
    					if((suser.length()==0 || slname.length()==0 || saddress.length()==0 || semail.length()==0 || sphone.length()==0 )){
        					JOptionPane.showMessageDialog(null,"Some Fields are empty","WARNING",JOptionPane.WARNING_MESSAGE);
    					}
    					
    								try{
    	
    				st= cn.createStatement();
    					
    				String query="insert into table1 values("+suser+",' "+slname+" ', "+saddress+"' "+semail+" ', "+saddress+")";
    				ps=cn.prepareStatement("INSERT INTO tbl_list " + " (FirstName,LastName,Address,Email,Phoneno) " + " VALUES(?,?,?,?,?)");
    				ps.setString(1,txtFName.getText());
    				ps.setString(2,txtLName.getText());
    				ps.setString(3,txtAddress.getText());
    				ps.setString(4,txtEmail.getText());
    				ps.setString(5,txtPhone.getText());
    							
    				ps.executeUpdate();
    							
    				JOptionPane.showMessageDialog(null,"Your New Client Info has been successfully Created.","client",JOptionPane.INFORMATION_MESSAGE);
    				txtFName.requestFocus(true);
    				st.close();
    
    					txtFName.setText("");
    					txtLName.setText("");
    					txtAddress.setText("");
    					txtEmail.setText("");
    					txtPhone.setText("");
    				
    					
    				}
    				
    				catch(SQLException sqlEx){
    				JOptionPane.showMessageDialog(null,"General error","client",JOptionPane.INFORMATION_MESSAGE);
    				}
    						
    					}
    				}
    				
    					}
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Dear pranayf, you have opened 3 posts with (nearly) the same code.
    One got deleted because of illegal double-posting, the other is: http://bytes.com/topic/java/answers/...ity-pleasehelp.

    In none of these posts you have give us the needed info to help you, even after I requested it. Why, what is the problem?

    I already asked two times for the error descripion. The error that you get on the screen (when trying to insert the records in line 147 to 155 on this code listing.)
    When you cannot insert records, it could be because the table name or fields are misspelled, you do not have the appropriate database rights, a database field has the wrong type, etc. It could have very many reasons and each reason needs a different solution.
    This is not a guessing game. We cannot help you if you do not give more information here (that is the exact error message that was shown to you)

    Comment

    • Sowrabh
      New Member
      • May 2014
      • 1

      #3
      Could you please share your logs for the error you are getting.

      Comment

      Working...