The Artima Developer Community
Sponsored Link

Java Answers Forum
Need helps in Java+SQL

2 replies on 1 page. Most recent reply: Aug 1, 2002 11:11 AM by Don Hill

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
chuck

Posts: 9
Nickname: chuckjava2
Registered: Jul, 2002

Need helps in Java+SQL Posted: Aug 1, 2002 1:08 AM
Reply to this message Reply
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();}
}
}


Jonathan Paul

Posts: 5
Nickname: jona
Registered: Jul, 2002

Re: Need helps in Java+SQL Posted: Aug 1, 2002 3:47 AM
Reply to this message Reply
Use simple string concatenation
e.g:

String var="tms";
int var2=1;

String query = "INSERT INTO TABLENAME VALUES ("+ var2 + ",'"+ var +"'";

Note the usage of String and int
Jona

Don Hill

Posts: 70
Nickname: ssswdon
Registered: Jul, 2002

Re: Need helps in Java+SQL Posted: Aug 1, 2002 11:11 AM
Reply to this message Reply
A better way would be to use a preparedstatement
like this, this creates a compiled object on the db and over time is better performing.

PreparedStatement ps = conn.prepareStatement("INSERT INTO StudentRecord VALUES (? ,? ,?, ?)");

then set the values
ps.setString(1, p1);
ps.setInteger( 2, p2 );

ps.execute();

HTH

Flat View: This topic has 2 replies on 1 page
Topic: java programming using TerminalIO  and BreezySwing interface Previous Topic   Next Topic Topic: how to create, store and use a graphic object...


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us