wood burning stoves 2.0*
The moose likes JDBC and the fly likes Data input from app to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Reply locked New topic
Author

Data input from app to database

egbai mmumene
Ranch Hand

Joined: Sep 18, 2012
Posts: 43
I'v been working on a movie library but every time i add a movie name in my app i get an error message sayin unknown column which is usually d name i put in to my app e.g if i add movie name: spiderman i get error message (unknown column 'spider man' in 'field list')
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

It sounds like you are building up aa select statement, and are not putting the values in quotes it should end up looking like

Insert into (...) values ('spiderman');
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

and welcome the the ranch
egbai mmumene
Ranch Hand

Joined: Sep 18, 2012
Posts: 43
tnx but its still not working. heres the code
package mm.main;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.FlowLayout;
import java.sql.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author smile
*/
public class moviemethod extends JFrame implements ActionListener{
private JButton entry;
private JLabel entrylabel;
private JTextField entryno;
private JTextField entrytext;
DBOperator login;
Statement stat;
ResultSet rs;
public moviemethod(){
}
public void entry(){
setVisible(true);
setLayout( new FlowLayout() );
Container pane = getContentPane();
setSize( 400, 200 );
entrylabel = new JLabel( "Movie name" );
entrytext = new JTextField(15);
entryno = new JTextField(3);
entry = new JButton( "enter" );
pane.add( entrylabel );
pane.add( entrytext );
pane.add( entryno );
pane.add( entry );
entry.addActionListener(this);
}

public void actionPerformed(ActionEvent event){
Object source = event.getSource();
if(source == entry){
try{
login = new DBOperator();
stat = login.getstatement();
String entrytxt;
int etxt = Integer.parseInt(entryno.getText());
entrytxt = entrytext.getText();
rs = stat.executeQuery("SELECT entryname, entryno FROM users");
stat.executeUpdate("INSERT INTO users (entryname, entryno) " +
"VALUES(" + entrytxt + ", '" + etxt + "')");
}
catch( SQLException ex ){
ex.printStackTrace();
}




}
}
}
egbai mmumene
Ranch Hand

Joined: Sep 18, 2012
Posts: 43
need help
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I would definitely recommend that you use a PreparedStatement rather than a Statement. There are several reasons why you should do that except when it's absolutely impossible to arrange it, and one of them is that you don't have horrible code full of two different kinds of quotes which it's easy to get wrong.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

stat.executeUpdate("INSERT INTO users (entryname, entryno) " +
"VALUES(" + entrytxt + ", '" + etxt + "')");

I am presuming entrytxt is a varchar and you don't have ' ' around it
egbai mmumene
Ranch Hand

Joined: Sep 18, 2012
Posts: 43
Tnx that error has been defeated but I'm getting an error message saying entry to long for column' entryname' really frustrated
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

look at the database how long is the entryname column, now what are you trying to insert?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data input from app to database