SQL/Java Question

Dominyo

Registered
I'm trying to make a simple JSP page which will post data from a form into a database (Hypersonic SQL). The following code:

Code:
		Connection con = DriverManager.getConnection("jdbc:hsqldb:/members/QxUdPuc5YiTgxPUGg5PJY8cJ6OahZ0jA/db/codepasterdb", "sa", "");
		
		String author1 = request.getParameter("name");
		String title1 = request.getParameter("title");
		String code1 = request.getParameter("code");
		java.sql.Date theDate1 = new java.sql.Date(2003, 11, 23);

		Statement stmt = con.createStatement();
		stmt.execute("INSERT INTO Snippets (Author, DatePosted, Title, Code) VALUES('author1', 'theDate1', 'title1', 'code1')");
		
		stmt.close();
		con.close();

Gets me this error:

SQL Exception: General error java.lang.IllegalArgumentException in statement [INSERT INTO Snippets (Author, DatePosted, Title, Code) VALUES('author1', 'theDate1', 'title1', 'code1') ]Thank you for submitting a snippet. Return to Snippet List

Anyone know what's going on?
 
Your VALUES list just contains literal strings, not the actual values of the Java variables you setup before. The error is caused by the fact that you assign the literal string 'theDate1' to a data value. You must provide a data literal like {d '2003-11-25'} to avoid this error.
 
You could do it with a PreparedStatement like this...

Connection con = DriverManager.getConnection(...);
String sqlStr = "INSERT INTO Snippets (Author, DatePosted, Title, Code) VALUES(?, ?, ?, ?)";
PreparedStatement ps = con.prepareStatement(sqlStr);
ps.setString(1, authorl);
ps.setString(2, theDatel);
ps.setString(3, titlel);
ps.setString(4, codel);
ResultSet rs = ps.executeUpdate();
ps.close();
con.close;
 
Back
Top