| | |
Java homework please help! :(
![]() |
•
•
Join Date: Dec 2007
Posts: 6
Reputation:
Solved Threads: 0
I coded a script, Currently I can add and delete items to the database but I cannot edit the data. Here's my 3 paged script :
Christmasswishlist.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
* Servlet implementation class for Servlet: ChristmasWishlist
*
*/
public class ChristmasWishlist extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet
{
static final long serialVersionUID = 1L;
/*
* Connect to the database
*/
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public ChristmasWishlist()
{
super();
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
process(request, response);
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
process(request, response);
}
protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><title>List of All Your Wishes</title><body>");
//check if you are getting the right values, comment out once the program is working
//line below will work only for java 5 up
java.util.Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
String gift = e.nextElement().toString();
out.println("<br>" + gift + " - " + request.getParameter(gift));
}
if (request.getParameter("submit") != null)
{
if (!request.getParameter("submit").isEmpty())
{
if (request.getParameter("submit").equals("Add"))
{
if (!request.getParameter("gift").isEmpty())
{
out.println(addChristmasWishlist(request
.getParameter("gift")));
}
else
{
out.println("<p>Nothing to do");
}
}
else if(request.getParameter("submit").equals("Delete"))
{
delChristmasWishlist(Integer.parseInt(request.getParameter("id")));
}
else if(request.getParameter("submit").equals("Delete"))
{
out.println("<p>Nothing to do");
}
}
} else
{
out.println("<p>empty");
}
out.println("<p><a href=wish.html>Add again</a>");
ResultSet rst = null;
try
{
rst = viewChristmasWishlist();
int count = 1;
out.println("<table><tr><th colspan=2>ChristmasWishlist</th></tr>");
while(rst.next())
{
out.println("<tr><td><b>"+count+".</b></td><td>"+rst.getString("gift")+"</td>" +
"<td><a href=ChristmasWishlist?id="+rst.getInt("id")+"&submit=Edit>Edit</a.</td>"+
"<td><a href=ChristmasWishlist?id="+rst.getInt("id")+"&submit=Delete>Delete</a.</td></tr>");
count++;
}
out.println("</table>");
}
catch(Exception e2)
{
}
out.println("</body></html>");
}
public String addChristmasWishlist(String wish)
{
try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "insert into wishes set gift='" + wish.replaceAll("\'", "\\\'") + "'";
if (stmt.execute(sql))
{
msg = "Insert failed";
}
else
{
msg = "Added successfully";
}
con.logOff();
return msg;
}
catch (SQLException e)
{
return "Add failed. SQL error " + e.getMessage();
}
catch (java.lang.NullPointerException ne)
{
return "<?p>Null error: " + ne.getMessage();
}
}
public String delChristmasWishlist(int id)
{
try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "delete from wishes where id="+id;
if (stmt.execute(sql))
{
msg = "Delete failed";
} else
{
msg = "Delete successfully";
}
con.logOff();
return msg;
} catch (SQLException e)
{
return "Add failed. SQL error " + e.getMessage();
} catch (java.lang.NullPointerException ne)
{
return "<p>Null error: " + ne.getMessage();
}
}
public String editChristmasWishlist(String wish)
{
try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "update from wishes where gift="+wish;
if (stmt.execute(sql))
{
msg = "update failed";
} else
{
msg = "update successfully";
}
con.logOff();
return msg;
} catch (SQLException e)
{
return "Add failed. SQL error " + e.getMessage();
} catch (java.lang.NullPointerException ne)
{
return "<p>Null error: " + ne.getMessage();
}
}
public ResultSet viewChristmasWishlist()
{
ResultSet rst = null;
try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "select * from wishes";
rst = stmt.executeQuery(sql);
} catch (SQLException e)
{
} catch (java.lang.NullPointerException ne)
{
}
return rst;
}
Connectionmanager.java
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionManager {
protected Connection con;
protected String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String database = "WishList";
String user = "root";
String password = "";
/** Creates a new instance of ConnectionManager */
public ConnectionManager() {
}
public Connection logOn(){
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+database,user,password);
}
catch(Exception e){
System.out.print(e.getMessage());
}
return con;
}
public void logOff(){
try {
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}
Wish.html
<!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=ISO-8859-1">
<title>Christmas Wishes</title>
</head>
<body>
<h3>Place Your Christmas Wishes Here</h3>
<form action="ChristmasWishlist" method=post>
<table>
<tr><th>Wish:</th><td><input type=text name=gift size=100></td></tr>
<tr><th>Amount:</th><td><input type=text name=amount size=10></td></tr>
<tr><th colspan=2><input type=submit name=submit value="Add"></th></tr>
</table>
</form>
</body>
</html>
Christmasswishlist.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
* Servlet implementation class for Servlet: ChristmasWishlist
*
*/
public class ChristmasWishlist extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet
{
static final long serialVersionUID = 1L;
/*
* Connect to the database
*/
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public ChristmasWishlist()
{
super();
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
process(request, response);
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
process(request, response);
}
protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><title>List of All Your Wishes</title><body>");
//check if you are getting the right values, comment out once the program is working
//line below will work only for java 5 up
java.util.Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
String gift = e.nextElement().toString();
out.println("<br>" + gift + " - " + request.getParameter(gift));
}
if (request.getParameter("submit") != null)
{
if (!request.getParameter("submit").isEmpty())
{
if (request.getParameter("submit").equals("Add"))
{
if (!request.getParameter("gift").isEmpty())
{
out.println(addChristmasWishlist(request
.getParameter("gift")));
}
else
{
out.println("<p>Nothing to do");
}
}
else if(request.getParameter("submit").equals("Delete"))
{
delChristmasWishlist(Integer.parseInt(request.getParameter("id")));
}
else if(request.getParameter("submit").equals("Delete"))
{
out.println("<p>Nothing to do");
}
}
} else
{
out.println("<p>empty");
}
out.println("<p><a href=wish.html>Add again</a>");
ResultSet rst = null;
try
{
rst = viewChristmasWishlist();
int count = 1;
out.println("<table><tr><th colspan=2>ChristmasWishlist</th></tr>");
while(rst.next())
{
out.println("<tr><td><b>"+count+".</b></td><td>"+rst.getString("gift")+"</td>" +
"<td><a href=ChristmasWishlist?id="+rst.getInt("id")+"&submit=Edit>Edit</a.</td>"+
"<td><a href=ChristmasWishlist?id="+rst.getInt("id")+"&submit=Delete>Delete</a.</td></tr>");
count++;
}
out.println("</table>");
}
catch(Exception e2)
{
}
out.println("</body></html>");
}
public String addChristmasWishlist(String wish)
{
try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "insert into wishes set gift='" + wish.replaceAll("\'", "\\\'") + "'";
if (stmt.execute(sql))
{
msg = "Insert failed";
}
else
{
msg = "Added successfully";
}
con.logOff();
return msg;
}
catch (SQLException e)
{
return "Add failed. SQL error " + e.getMessage();
}
catch (java.lang.NullPointerException ne)
{
return "<?p>Null error: " + ne.getMessage();
}
}
public String delChristmasWishlist(int id)
{
try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "delete from wishes where id="+id;
if (stmt.execute(sql))
{
msg = "Delete failed";
} else
{
msg = "Delete successfully";
}
con.logOff();
return msg;
} catch (SQLException e)
{
return "Add failed. SQL error " + e.getMessage();
} catch (java.lang.NullPointerException ne)
{
return "<p>Null error: " + ne.getMessage();
}
}
public String editChristmasWishlist(String wish)
{
try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "update from wishes where gift="+wish;
if (stmt.execute(sql))
{
msg = "update failed";
} else
{
msg = "update successfully";
}
con.logOff();
return msg;
} catch (SQLException e)
{
return "Add failed. SQL error " + e.getMessage();
} catch (java.lang.NullPointerException ne)
{
return "<p>Null error: " + ne.getMessage();
}
}
public ResultSet viewChristmasWishlist()
{
ResultSet rst = null;
try
{
ConnectionManager con = new ConnectionManager();
Statement stmt = con.logOn().createStatement();
String msg = "";
String sql = "select * from wishes";
rst = stmt.executeQuery(sql);
} catch (SQLException e)
{
} catch (java.lang.NullPointerException ne)
{
}
return rst;
}
Connectionmanager.java
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionManager {
protected Connection con;
protected String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String database = "WishList";
String user = "root";
String password = "";
/** Creates a new instance of ConnectionManager */
public ConnectionManager() {
}
public Connection logOn(){
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+database,user,password);
}
catch(Exception e){
System.out.print(e.getMessage());
}
return con;
}
public void logOff(){
try {
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}
Wish.html
<!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=ISO-8859-1">
<title>Christmas Wishes</title>
</head>
<body>
<h3>Place Your Christmas Wishes Here</h3>
<form action="ChristmasWishlist" method=post>
<table>
<tr><th>Wish:</th><td><input type=text name=gift size=100></td></tr>
<tr><th>Amount:</th><td><input type=text name=amount size=10></td></tr>
<tr><th colspan=2><input type=submit name=submit value="Add"></th></tr>
</table>
</form>
</body>
</html>
- No one is going to look at that without [code][/code] tags
- And you need to pinpoint a specific issue you are having, not just post your entire code
If anyone else wants to look...
Java Syntax (Toggle Plain Text)
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.*; /** * Servlet implementation class for Servlet: ChristmasWishlist * */ public class ChristmasWishlist extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; /* * Connect to the database */ /* * (non-Java-doc) * * @see javax.servlet.http.HttpServlet#HttpServlet() */ public ChristmasWishlist() { super(); } /* * (non-Java-doc) * * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, * HttpServletResponse response) */ protected void doGet ( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { // TODO Auto-generated method stub process ( request, response ); } /* * (non-Java-doc) * * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, * HttpServletResponse response) */ protected void doPost ( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { // TODO Auto-generated method stub process ( request, response ); } protected void process ( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { response.setContentType ( "text/html" ); java.io.PrintWriter out = response.getWriter(); out.println ( "<html><title>List of All Your Wishes</title><body>" ); //check if you are getting the right values, comment out once the program is working //line below will work only for java 5 up java.util.Enumeration e = request.getParameterNames(); while ( e.hasMoreElements() ) { String gift = e.nextElement().toString(); out.println ( "<br>" + gift + " - " + request.getParameter ( gift ) ); } if ( request.getParameter ( "submit" ) != null ) { if ( !request.getParameter ( "submit" ).isEmpty() ) { if ( request.getParameter ( "submit" ).equals ( "Add" ) ) { if ( !request.getParameter ( "gift" ).isEmpty() ) { out.println ( addChristmasWishlist ( request .getParameter ( "gift" ) ) ); } else { out.println ( "<p>Nothing to do" ); } } else if ( request.getParameter ( "submit" ).equals ( "Delete" ) ) { delChristmasWishlist ( Integer.parseInt ( request.getParameter ( "id" ) ) ); } else if ( request.getParameter ( "submit" ).equals ( "Delete" ) ) { out.println ( "<p>Nothing to do" ); } } } else { out.println ( "<p>empty" ); } out.println ( "<p><a href=wish.html>Add again</a>" ); ResultSet rst = null; try { rst = viewChristmasWishlist(); int count = 1; out.println ( "<table><tr><th colspan=2>ChristmasWishlist</th></tr>" ); while ( rst.next() ) { out.println ( "<tr><td><b>" + count + ".</b></td><td>" + rst.getString ( "gift" ) + "</td>" + "<td><a href=ChristmasWishlist?id=" + rst.getInt ( "id" ) + "&submit=Edit>Edit</a.</td>" + "<td><a href=ChristmasWishlist?id=" + rst.getInt ( "id" ) + "&submit=Delete>Delete</a.</td></tr>" ); count++; } out.println ( "</table>" ); } catch ( Exception e2 ) { } out.println ( "</body></html>" ); } public String addChristmasWishlist ( String wish ) { try { ConnectionManager con = new ConnectionManager(); Statement stmt = con.logOn().createStatement(); String msg = ""; String sql = "insert into wishes set gift='" + wish.replaceAll ( "\'", "\\\'" ) + "'"; if ( stmt.execute ( sql ) ) { msg = "Insert failed"; } else { msg = "Added successfully"; } con.logOff(); return msg; } catch ( SQLException e ) { return "Add failed. SQL error " + e.getMessage(); } catch ( java.lang.NullPointerException ne ) { return "<?p>Null error: " + ne.getMessage(); } } public String delChristmasWishlist ( int id ) { try { ConnectionManager con = new ConnectionManager(); Statement stmt = con.logOn().createStatement(); String msg = ""; String sql = "delete from wishes where id=" + id; if ( stmt.execute ( sql ) ) { msg = "Delete failed"; } else { msg = "Delete successfully"; } con.logOff(); return msg; } catch ( SQLException e ) { return "Add failed. SQL error " + e.getMessage(); } catch ( java.lang.NullPointerException ne ) { return "<p>Null error: " + ne.getMessage(); } } public String editChristmasWishlist ( String wish ) { try { ConnectionManager con = new ConnectionManager(); Statement stmt = con.logOn().createStatement(); String msg = ""; String sql = "update from wishes where gift=" + wish; if ( stmt.execute ( sql ) ) { msg = "update failed"; } else { msg = "update successfully"; } con.logOff(); return msg; } catch ( SQLException e ) { return "Add failed. SQL error " + e.getMessage(); } catch ( java.lang.NullPointerException ne ) { return "<p>Null error: " + ne.getMessage(); } } public ResultSet viewChristmasWishlist() { ResultSet rst = null; try { ConnectionManager con = new ConnectionManager(); Statement stmt = con.logOn().createStatement(); String msg = ""; String sql = "select * from wishes"; rst = stmt.executeQuery ( sql ); } catch ( SQLException e ) { } catch ( java.lang.NullPointerException ne ) { } return rst; }
Connectionmanager.java
Java Syntax (Toggle Plain Text)
import java.sql.Connection; import java.sql.DriverManager; public class ConnectionManager { protected Connection con; protected String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/"; String database = "WishList"; String user = "root"; String password = ""; /** Creates a new instance of ConnectionManager */ public ConnectionManager() { } public Connection logOn() { try { Class.forName ( driver ).newInstance(); con = DriverManager.getConnection ( url + database, user, password ); } catch ( Exception e ) { System.out.print ( e.getMessage() ); } return con; } public void logOff() { try { con.close(); } catch ( Exception e ) { e.printStackTrace(); } } }
Wish.html
Java Syntax (Toggle Plain Text)
<!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=ISO-8859-1"> <title>Christmas Wishes</title> </head> <body> <h3>Place Your Christmas Wishes Here</h3> <form action="ChristmasWishlist" method=post> <table> <tr><th>Wish:</th><td><input type=text name=gift size=100></td></tr> <tr><th>Amount:</th><td><input type=text name=amount size=10></td></tr> <tr><th colspan=2><input type=submit name=submit value="Add"></th></tr> </table> </form> </body> </html>
Last edited by iamthwee; Dec 19th, 2007 at 4:07 pm.
*Voted best profile in the world*
Are you being able to execute "update from wishes where gift=<giftid>" via command prompt ?
![]() |
Similar Threads
- We only give homework help to those who show effort (Computer Science)
- I need help with doing my Java homework. (Java)
- Need help with Caesar Cipher program (Java)
- java---->c++ (Java)
- java and using observable class (Java)
- hijacked please help, i can't do homework (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: Need help in Programming
- Next Thread: Java Servlet Problem :(
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class client code compile compiler component database developmenthelp eclipse error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loops mac main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying number online pearl problem program programming project qt recursion scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






