chuck
Posts: 9
Nickname: chuckjava2
Registered: Jul, 2002
|
|
Need helps in Java+SQL
|
Posted: Jul 31, 2002 10:08 PM
|
|
Advertisement
|
I don't know how to write an SQL statement that takes user inputs. Please help. Pay attention in this line
("INSERT INTO StudentRecord VALUES (p1 ,p2 ,p3, p4)");
Thank you very much.
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import sun.jdbc.odbc.*;
public class Assign5th extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Student Records"; String p1, p2, p3, p4; p1 =request.getParameter("param1"); p2 =request.getParameter("param2"); p3 =request.getParameter("param3"); p4 =request.getParameter("param4");
out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=CENTER>" + title + "</H1>\n" + "<UL>\n" + " <LI><B>Student ID</B>: " + p1 + "\n" + " <LI><B>Last Name</B>: " + p2 + "\n" + " <LI><B>First Name</B>: " + p3 + "\n" + " <LI><B>Final Grade</B>: " + p4 + "\n" + "</UL>\n" + "</BODY></HTML>");
try{ //Load drivers new JdbcOdbcDriver(); String url = "jdbc:odbc:Sales";
//Connect to database String user = ""; String password = ""; Connection con = DriverManager.getConnection(url, user, password); Statement stmt = con.createStatement();
//Create and load tables stmt.executeUpdate("CREATE TABLE StudentRecord (StudentID VARCHAR(4), LastName" + " VARCHAR(25), FirstName VARCHAR(25), TotalGrade VARCHAR(1))"); stmt.executeUpdate ("INSERT INTO StudentRecord VALUES (p1 ,p2 ,p3, p4)"); stmt.close(); } catch (SQLException se){System.out.println("SQL Exception"); } catch (Exception e) {e.printStackTrace();} } }
|
|