Database error
I have a database error in the following program.
The program and the error is below
help me i am new to jdbc conceps
The Program is to update a table using user inputs ( TO create a new row in the table and then following diffrent functions i.e to alter and delete the rows etc)
import java.util.*;
import java.lang.*;
import java.net.*;
import java.sql.*;
import java.io.*;
class Dbman
{
public static void main(String args[])
{
try
{
String url = "jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,"root", "shasi");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery ("Select * FROM element");
while (rs.next())
{
String name = rs.getString("Name");
String age = rs.getString("Age");
String address = rs.getString("ADDRESS");
System.out.println(name + "\t" + age + "\t" + address);
}
BufferedReader nin = new BufferedReader(new InputStreamReader(System.in));
BufferedReader ain = new BufferedReader(new InputStreamReader(System.in));
BufferedReader adin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name: ");
String newname = nin.readLine();
System.out.println("Enter the age: ");
String newage = ain.readLine();
System.out.println("Enter the address: ");
String newaddress = adin.readLine();
ResultSetMetaData rsmd = rs.getMetaData ();
PreparedStatement stmt;
stmt = con.prepareStatement("insert into element(Name,Age,ADDRESS) values (?)");
stmt.setCharacterStream(1, new StringReader(newname),0);
stmt.setCharacterStream(1, new StringReader(newname),1);
stmt.setCharacterStream(1, new StringReader(newname),2);
stmt.executeUpdate();
/*
PreparedStatement stmt1;
stmt1 = con.prepareStatement("UPDATE element SET (Age = ? Where Name = ? ");
stmt1.setCharacterStream(1, new StringReader(newage), 0);
// stmt1.setCharacterStream(1, new StringReader(newaddress),0);
PreparedStatement stmt2;
stmt2 = con.prepareStatement("UPDATE element SET (ADDRESS = ? WHERE Name LIKE ?");
stmt2.setCharacterStream(1, new StringReader(newaddress),0);
stmt1.executeUpdate();
stmt2.executeUpdate();
*/
/* s.executeUpdate ("Update element Set(Age=stmt.setCharacterStream(1, new StringReader(newage),0),ADDRESS=stmt.setCharacterStream(1, new StringReader(newaddress),0) WHERE Name= stmt.setCharacterStream(1, new StringReader(newname),0)");
*/
//int nrows = s.executeUpdate ("INSERT INTO element(Age,Address) //VALUES(newname,newage,newaddress)");
rs = s.executeQuery ("SELECT * FROM element");
while (rs.next())
{
String name = rs.getString("Name");
String age = rs.getString("Age");
String address = rs.getString("ADDRESS");
System.out.println(name + "\t" + age + "\t" + address);
}
rs.close();
s.close();
con.close();
}
catch(Exception c)
{
c.printStackTrace();
}
}
}
THE ERROR
java.sql.SQLException: Column count doesn't match value count at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.jav
a:936)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:
1166)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:
1082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:
1067)
at Dbman.main(Dbman.java:51)
thankyou