1 Attachment(s)
What is wrong with my CODE???:%
Attachment 3848
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;
}
}
Re: What is wrong with my CODE???:%
Re: What is wrong with my CODE???:%
database?? could you expand your answer a little thanks!
Re: What is wrong with my CODE???:%
Quote:
Originally Posted by
davlipper
database?? could you expand your answer a little thanks!
Huh? What database?
Did you go through the linked pages?
db
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.
db
Re: What is wrong with my CODE???:%