0

Here is my code...

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class example1{
	public static void main(String[] args)
	{
		try
		{
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","user","password");
			Statement state = (Statement) con.createStatement();
			
			String username = "John";
			String password = "John1";
			int experience = 0;
			int level = 1;
			
			String insert = "INSERT INTO userInfo VALUES("+ username +","+ password +","+ experience +","+ level +").";
			
			state.executeUpdate(insert);
		
		}
		catch( Exception E )
		{ 
			System.out.println( E.getMessage() );
		}
	}
}

I'm getting this error


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.' at line 1.

How do I fix this?


Thanks tons

Peace and love,
oldezwe

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by vadriaan
0

I Think i fixed that, but...

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class example1{
	public static void main(String[] args)
	{
		try
		{
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","user","password");
			Statement state = (Statement) con.createStatement();
			
			String username = "John";
			String password = "John1";
			int experience = 0;
			int level = 1;
			
			String insert = "INSERT INTO userInfo VALUES("+ username +","+ password +","+ experience +","+ level +")";
			state.executeUpdate(insert);
		
		}
		catch( Exception E )
		{ 
			System.out.println( E.getMessage() );
		}
	}
}

I am now getting this error...

Unknown column 'John' in 'field list' ?

0

If you look at the value of 'insert' the statement will read:

INSERT INTO userInfo VALUES(John,John1,0,1)

John and John1 is interpreted as column names since they are not integers.
Try placing the values inside ''.
e.g. VALUES('" + username + "',etc will be read as VALUES('John',

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.