Написал код (почти сам :D):
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/gb","root","zaz222");
%>
<h1>My first FORM</h1>
<form method="POST">
<input name='name' type='text' maxlength='30'><br>
<textarea name="text" rows="4" cols="20"></textarea>
<input type='submit' value='OK'>
</form>
<br><br><br>
<%
String name=request.getParameter("name");
String text=request.getParameter("text");
Statement stmt=conn.createStatement();
if(name!=null && text!=null)
{
int id_user=0;
ResultSet rs=stmt.executeQuery("SELECT ID FROM user WHERE NAME='"+name+"'");
rs.next();
id_user=rs.getInt("ID");
if(id_user!=0)
{
stmt.executeUpdate("INSERT INTO message SET TEXT='"+text+"',ID_USER='"+id_user+"',TIME=NOW()");
}
rs.close();
}
out.print("<br><br>");
ResultSet mess=stmt.executeQuery("SELECT * FROM message M,user U WHERE U.ID=M.ID_USER");
while(mess.next())
{
out.print("Name: "+mess.getString("NAME")+"<br>");
out.print("Time: "+mess.getString("TIME")+"<br>");
out.print("Message: "+mess.getString("TEXT")+"<br>");
out.print("<hr>");
}
mess.close();
%>
</body>
</html>
<%
stmt.close();
conn.close();
%>
HTTP Status 500 -
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.sql.SQLException: Illegal operation on empty result set.
root cause
java.sql.SQLException: Illegal operation on empty result set.
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.
GlassFish Server Open Source Edition 3.0.1
P.S За книгой по MySQL пойду в воскресенье :)