Results 1 to 7 of 7
Thread: What is wrong with my CODE???:%
- 06-05-2012, 12:10 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
What is wrong with my CODE???:%

Hi guys, new to this forum so hope I can get this sorted:)
My problem is when a company creates a new job in this web application,the action should update the list of jobs already in the table...now this is working fine except as you may notice the new job created has ID = 0 after the job has been created.How can I get this ID to show up?my action called is:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
public class ApplyForJob {
private String username;
private List<Job> jobs;//obtained from session
private String jobName;
private String skillName;
private Skill skill;
private int IDjob;
private String startDate;
private String endDate;
private String salary;
private String description;
private String result ="SUCCESS";
private Map sessions;
private Connection connection;
private PreparedStatement createCandidateJob;
private PreparedStatement updateJob;
private Statement generateJobs;
private ResultSet jobSet;
public ApplyForJob() {
}
private void loadDriver() throws ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
}
// Setup the connection with the DB
private void makeConnection() throws SQLException {
connection=DriverManager.getConnection("jdbc:mysql ://localhost/recruit?user=root&password=test123");
}
//execute update
private void createJobUpdate () throws SQLException {
System.out.println("create job update called");
sessions=ActionContext.getContext().getSession();
username= (String) sessions.get("username");
//the current user in the session
String query = ("insert into recruit.candidatejob (username,IDjob) values(?,?)");
this.createCandidateJob = connection.prepareStatement(query);
this.createCandidateJob.setString(1,username);
this.createCandidateJob.setInt(2, this.getIDjob());
this.JobQuery();//find out the current users new contacts(update contactSet)
System.out.println("after jobquery");
this.jobs = new ArrayList<Job>();//initialise the userContacts list
while(this.jobSet.next()) {//populate jobs for the user who logged in
String jobName =jobSet.getString("jobName");
String startDate =jobSet.getString("startDate");
String endDate =jobSet.getString("endDate");
String salary =jobSet.getString("salary");
String description =jobSet.getString("description");
String skillName =jobSet.getString("skillName");
System.out.println("helloo");
this.jobs.add(new Job(IDjob,jobName,startDate,endDate,salary,descrip tion,skillName,username));
}
Collections.sort(jobs);
sessions.put("jobs", this.jobs);
sessions.put("username", getUsername());//overwrite contacts attribute in sessions
System.out.println(jobs.size());
}
public void JobQuery() throws SQLException {
this.generateJobs=connection.createStatement();//build the generateContacts statement
String sql ="SELECT * FROM job LEFT OUTER JOIN candidatejob on job.IDjob = candidatejob.IDjob AND candidateJob.username = '"+username+"' where candidatejob.IDjob IS null";
System.out.println(username);
jobSet= this.generateJobs.executeQuery(sql);
System.out.println("inner join query");
}
public String execute() throws Exception {
boolean validJob = true;
try {
this.loadDriver();
this.makeConnection();
System.out.println("in try");
this.createJobUpdate();
if(validJob) {
this.createCandidateJob.executeUpdate();//adds the new job to the database
//get "contacts" session attribute here and store in userContacts list
Job job = new Job(this.getIDjob(),this.getJobName(), this.getStartDate(),this.getEndDate(),this.getSala ry(),this.getDescription(),this.getSkillName(),use rname);
this.jobs=(List)sessions.get("jobs");//the current users jobs
this.jobs.add(job);
//update the sessions list here
Collections.sort(jobs);
sessions.put("jobs", jobs);//overwrite contacts attribute in sessions
System.out.println("after jobs session");
}
}
finally{
this.closeConnections();
return result;
}
}
/**
* Closes prepared statements and the connection to the database.
*/
public void closeConnections() {
try {
this.createCandidateJob.close();
connection.close();
}
catch (SQLException e) {
System.out.println("");
e.printStackTrace();
}
}
/**
* @return the contactname
*/
public String getJobName() {
return jobName;
}
public String getUsername() {
return username;
}
/**
* @param contactname the contactname to set
*/
public void setJobName(String jobName) {
this.jobName = jobName;
}
/**
* @return the phonenumber
*/
public String getStartDate() {
return startDate;
}
public String getSkillName() {
return skillName;
}
public void setSkillName(String skillName) {
this.skillName = skillName;
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
System.out.println("set skill called ");
this.skill = skill;
}
/**
* @param phonenumber the phonenumber to set
*/
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
/**
* @param phonenumber the phonenumber to set
*/
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getSalary() {
return salary;
}
/**
* @param phonenumber the phonenumber to set
*/
public void setSalary(String salary) {
this.salary = salary;
}
public String getDescription() {
return description;
}
/**
* @param phonenumber the phonenumber to set
*/
public void setDescription(String description) {
this.description = description;
}
public void setIDjob(int IDjob) {
this.IDjob = IDjob;
}
public int getIDjob() {
return IDjob;
}
}
- 06-05-2012, 12:19 PM #2
Re: What is wrong with my CODE???:%
Why do they call it rush hour when nothing moves? - Robin Williams
- 06-05-2012, 12:23 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
Re: What is wrong with my CODE???:%
database?? could you expand your answer a little thanks!
- 06-05-2012, 12:36 PM #4
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
Update Action Problem:(
Moderator edit: Merged from a double post.
Hi, I have a list of jobs in a job portal that a user may apply for! When the user logs into the application they are provided with all the jobs in the database that they have NOT applied for yet.When the user clicks an applyForJob button,the following action is called:
My problem is that the list of updated jobs which should include 1 less job now that they have applied for the job, does not show up at all:(
I think maybe it could be in the resulted .jsp page where my problem lies...help much appreciated!!! Also when the user logs in struts directs me to welcome.jsp! When the user applys for a job and the following action is called I have again directed struts to welcome.jsp ! This isnt a problem though or is it?!?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
public class ApplyForJob {
private String username;
private List<Job> jobs;//obtained from session
private String jobName;
private String skillName;
private Skill skill;
private int IDjob;
private String startDate;
private String endDate;
private String salary;
private String description;
private String result ="SUCCESS";
private Map sessions;
private Connection connection;
private PreparedStatement createCandidateJob;
private PreparedStatement updateJob;
private Statement generateJobs;
private ResultSet jobSet;
public ApplyForJob() {
}
private void loadDriver() throws ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
}
// Setup the connection with the DB
private void makeConnection() throws SQLException {
connection=DriverManager.getConnection("jdbc:mysql ://localhost/recruit?user=root&password=test123");
}
//execute update
private void createJobUpdate () throws SQLException {
System.out.println("create job update called");
sessions=ActionContext.getContext().getSession();
username= (String) sessions.get("username");
//the current user in the session
String query = ("insert into recruit.candidatejob (username,IDjob) values(?,?)");
this.createCandidateJob = connection.prepareStatement(query);
this.createCandidateJob.setString(1,username);
this.createCandidateJob.setInt(2, this.getIDjob());
this.JobQuery();//find out the current users new contacts(update contactSet)
System.out.println("after jobquery");
this.jobs = new ArrayList<Job>();//initialise the userContacts list
while(this.jobSet.next()) {//populate jobs for the user who logged in
String jobName =jobSet.getString("jobName");
String startDate =jobSet.getString("startDate");
String endDate =jobSet.getString("endDate");
String salary =jobSet.getString("salary");
String description =jobSet.getString("description");
String skillName =jobSet.getString("skillName");
System.out.println("helloo");
this.jobs.add(new Job(IDjob,jobName,startDate,endDate,salary,descrip tion,skillName,username));
}
Collections.sort(jobs);
sessions.put("jobs", this.jobs);
sessions.put("username", getUsername());//overwrite contacts attribute in sessions
System.out.println(jobs.size());
}
public void JobQuery() throws SQLException {
this.generateJobs=connection.createStatement();//build the generateContacts statement
String sql ="SELECT * FROM job LEFT OUTER JOIN candidatejob on job.IDjob = candidatejob.IDjob AND candidateJob.username = '"+username+"' where candidatejob.IDjob IS null";
System.out.println(username);
jobSet= this.generateJobs.executeQuery(sql);
System.out.println("inner join query");
}
public String execute() throws Exception {
boolean validJob = true;
try {
this.loadDriver();
this.makeConnection();
System.out.println("in try");
this.createJobUpdate();
if(validJob) {
this.createCandidateJob.executeUpdate();//adds the new job to the database
//get "contacts" session attribute here and store in userContacts list
Job job = new Job(this.getIDjob(),this.getJobName(), this.getStartDate(),this.getEndDate(),this.getSala ry(),this.getDescription(),this.getSkillName(),use rname);
this.jobs=(List)sessions.get("jobs");//the current users jobs
this.jobs.add(job);
//update the sessions list here
Collections.sort(jobs);
sessions.put("jobs", jobs);//overwrite contacts attribute in sessions
System.out.println("after jobs session");
}
}
finally{
this.closeConnections();
return result;
}
}
/**
* Closes prepared statements and the connection to the database.
*/
public void closeConnections() {
try {
this.createCandidateJob.close();
connection.close();
}
catch (SQLException e) {
System.out.println("");
e.printStackTrace();
}
}
/**
* @return the contactname
*/
public String getJobName() {
return jobName;
}
public String getUsername() {
return username;
}
/**
* @param contactname the contactname to set
*/
public void setJobName(String jobName) {
this.jobName = jobName;
}
/**
* @return the phonenumber
*/
public String getStartDate() {
return startDate;
}
public String getSkillName() {
return skillName;
}
public void setSkillName(String skillName) {
this.skillName = skillName;
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
System.out.println("set skill called ");
this.skill = skill;
}
/**
* @param phonenumber the phonenumber to set
*/
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
/**
* @param phonenumber the phonenumber to set
*/
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getSalary() {
return salary;
}
/**
* @param phonenumber the phonenumber to set
*/
public void setSalary(String salary) {
this.salary = salary;
}
public String getDescription() {
return description;
}
/**
* @param phonenumber the phonenumber to set
*/
public void setDescription(String description) {
this.description = description;
}
public void setIDjob(int IDjob) {
this.IDjob = IDjob;
}
public int getIDjob() {
return IDjob;
}
}
THE RESULTING JSP PAGE IS:
You are currently logged in as <s:property value="%{#session.username}"/>
</s:if>
<a href="<s:url action="Logout"/>">Click here to logout</a>
<s:if test="jobs.size >=1">
Here is a list of job vacancies that you can apply for:</br>
</s:if>
<s:if test="#session.jobs.isEmpty">
There is no Jobs available.
</s:if>
<p style = "float:centre;">
<table border="0">
<s:iterator value= "jobs">
<tr>
<tr>
<td>Job Name</td>
<td>
<s:property value="jobName"/>
</td>
</tr>
<tr>
<td>Start Date</td>
<td>
<s:property value="startDate"/>
</td>
</tr>
<tr>
<td>End Date</td>
<td>
<s:property value="endDate"/>
</td>
</tr>
<tr>
<td>Salary</td>
<td>
<s:property value="salary"/>
</td>
</tr>
<tr>
<td>Description</td>
<td>
<s:property value="description"/>
</td>
</tr>
<tr>
<td>Skill Type</td>
<td>
<s:property value="skillName"/>
</td>
</tr>
<tr>
<td>
<form action="applyForJob">
<s:hidden name="IDjob" />
<input type="submit" value="Apply"/>
</form>
</td>
</tr>
</tr><br>
</s:iterator>
<s:a action="jobCreate"> Add some jobs here!</s:a>
</table>
</p>
</body>
</html>Last edited by DarrylBurke; 06-05-2012 at 02:45 PM.
- 06-05-2012, 02:38 PM #5
- 06-05-2012, 02:44 PM #6
Re: What is wrong with my CODE???:%
Don't double post. I've merged the two threads.
Do go through links provided in responses, and don't continue to post unformatted code.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 06-05-2012, 02:45 PM #7
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
What is wrong with this code?
By gkoef in forum New To JavaReplies: 5Last Post: 05-29-2011, 05:31 PM -
Need help! something wrong in my code
By novak100 in forum New To JavaReplies: 2Last Post: 11-18-2009, 11:59 PM -
what's wrong with my code? please help me...
By lovely23 in forum Java AppletsReplies: 2Last Post: 03-01-2009, 01:33 PM -
what's wrong with this code?
By agenteleven in forum Advanced JavaReplies: 5Last Post: 10-07-2008, 11:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks