Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Column count doesn't match value count at row 1

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Column count doesn't match value count at row 1

    	public static boolean sentBan(String username, long time, String reason) {
    		DatabaseConnection connection = World.getConnectionPool().nextFree();
    		if (connection == null) 
    			return false;
    		try {
    			Statement stmt = connection.createStatement();
    			ResultSet user = stmt
    					.executeQuery("SELECT * FROM `vbulletin_user` WHERE username = '"
    							+ username + "'");
    			if (user.next()) {
    				int userId = user.getInt("userid");
    				int groupId = user.getInt("usergroupid");
    				int displaygroupid = 0;
    				String usertitle = user.getString("usertitle");
    				int customtitle = 0;
    				int adminid = 1;
    				long bandate = System.currentTimeMillis() / 1000;
    				long liftdate = time;
     
    				/* Inserting the bans */
     
    				String sql = "INSERT INTO vbulletin_userban (userid, usergroupid, displaygroupid, usertitle, customtitle, adminid, bandate, liftdate, reason) "
    						+ "VALUES ('"
    						+ userId
    						+ "', '"
    						+ groupId
    						+ "', '"
    						+ displaygroupid
    						+ "', '"
    						+ usertitle
    						+ "', '"
    						+ customtitle
    						+ "', '"
    						+ adminid
    						+ "', '"
    						+ bandate
    						+ "', '" + liftdate + "')";
    				stmt.execute(sql);
    				return true;
    			} 
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			if (connection != null) {
    				connection.returnConnection();
    			}
    		}
    		return false;
    	}

    I'm receiving this stacktrace when I try to use the above code.

    java.sql.SQLException: Column count doesn't match value count at row 1
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
    	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
    	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2713)
    	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2663)
    	at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:888)
    	at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:730)
    	at com.mysthalin.game.player.content.custom.ForumActions.sentBan(ForumActions.java:60)
    	at com.mysthalin.game.player.content.Commands.processModCommand(Commands.java:690)
    	at com.mysthalin.game.player.content.Commands.processCommand(Commands.java:68)
    	at com.mysthalin.networking.decoders.WorldPacketsDecoder.processPackets(WorldPacketsDecoder.java:1709)
    	at com.mysthalin.networking.decoders.WorldPacketsDecoder.decode(WorldPacketsDecoder.java:289)
    	at com.mysthalin.networking.ServerChannelHandler.messageReceived(ServerChannelHandler.java:98)
    	at org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88)
    	at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
    	at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:553)
    	at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
    	at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
    	at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:84)
    	at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processSelectedKeys(AbstractNioWorker.java:471)
    	at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:332)
    	at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
    	at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    	at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)

    This is the structure of the table:



    What am I doing wrong?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Column count doesn't match value count at row 1

    at com.mysthalin.game.player.content.custom.ForumActi ons.sentBan(ForumActions.java:60)
    So, which is line 60?

    What am I doing wrong?
    Perhaps there is no reason for the exception. More plainly, you have an INSERT statement specifying 9 columns, but you only specify 8 data values.

    I'm no database expert, but I often see people suggest you use PreparedStatement in place of those string concatenations. It protects against SQL injection attacks (if those might be a problem), but also gets the quoting right and adapts itself to the syntacical quirks of whatever dbms you are using.

Similar Threads

  1. Replies: 3
    Last Post: October 26th, 2011, 03:37 PM
  2. Replies: 5
    Last Post: October 19th, 2011, 08:21 PM
  3. java Loop program to place distinct value in a row, column and diagonal
    By javanovoice in forum Loops & Control Statements
    Replies: 3
    Last Post: September 8th, 2011, 08:08 PM
  4. [SOLVED] how to get the row and column of a component gridlayout
    By prettynew in forum AWT / Java Swing
    Replies: 0
    Last Post: March 13th, 2011, 06:39 PM
  5. Highlighting both row and column in JTable
    By bschneider14 in forum AWT / Java Swing
    Replies: 4
    Last Post: May 29th, 2010, 09:14 AM