Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        try
        {
            Class.forName("java.sql.Driver");
            
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel_petrichor","root","1234");
            int pday = 0;
         
           
            String rt;
            rt=c1.getSelectedItem().toString();
          
            if(rt.equals("General AC"))
                pday = 2000;
            else if(rt.equals("Delux"))
                pday = 2500;
            else if(rt.equals("Super Delux"))
                pday = 3500;
            else if(rt.equals("Special Suite"))
                pday = 5000;
             jt6.setText(""+pday);
            
         
             int book= Integer.parseInt (jt1.getText());
             String name= jt2.getText();
             long phone= Long.parseLong (jt3.getText());
             int rno = Integer.parseInt(jt4.getText());
             int days = Integer.parseInt(jt5.getText());
              
             int total= pday*days;
             jt7.setText(total+"");
              String query="insert into hotelp (bno,name,phone,rno,rtype,days,perday,total) values ( '"+book+"','"+name+"','"+phone+"','"+rno+"','"+days+"','"+pday+"','"+total+"');";
              Statement stmt;
              stmt = (Statement) con.createStatement();
              stmt.executeUpdate(query);
              
              JOptionPane.showMessageDialog(null,"Saved Successfully");
              
              stmt.close();
              con.close();
        }
        catch(Exception e1)
        {
              System.out.println(e1);
              JOptionPane.showMessageDialog(null,"error");
              
        }






create database hotel_petrichor;
use hotel_petrichor;
create table hotelp
(
bno int(5),
name varchar(20),
phone int(20),
rno int(4),
rtype varchar(20),
days int(2),
perday int(4),
total int(6)
);


What I have tried:

ive tried editing counting re-writing but i get the same message over and over agian
"java.sql.SQLException: Column count doesn't match value count at row 1"
Posted
Updated 28-Jun-18 0:29am
v2

Java
String query="insert into hotelp (bno,name,phone,rno,rtype,days,perday,total) values ( '"+book+"','"+name+"','"+phone+"','"+rno+"','"+days+"','"+pday+"','"+total+"');";

A quick count of the number of column names and the number of variables shows that you do not have a value for rtype.

If you used proper parameterised queries you would see this issue immediately.
 
Share this answer
 
Look at the code:
String query="insert into hotelp (bno,name,phone,rno,rtype,days,perday,total) values ( '"+book+"','"+name+"','"+phone+"','"+rno+"','"+days+"','"+pday+"','"+total+"');";
Now match up the data with the columns:
bno      book   
name     name   
phone    phone  
rno      rno    
rtype    days   
days     pday   
perday   total  
total
What happened to the rtype?

But don't do it like that!
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
C#
String query="insert into hotelp (bno,name,phone,rno,rtype,days,perday,total) values ( '"+book+"','"+name+"','"+phone+"','"+rno+"','"+days+"','"+pday+"','"+total+"');";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900