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

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' ?

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',

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.