Click here to Skip to main content
15,561,290 members
Home / Discussions / Java
   

Java

 
AnswerRe: Observer Pattern - Restart Thread Again When Exception Occurs Pin
Richard MacCutchan5-Jul-17 22:55
mveRichard MacCutchan5-Jul-17 22:55 
QuestionNo value specified for parameter 2 Pin
karengsh3-Jul-17 6:27
karengsh3-Jul-17 6:27 
AnswerRe: No value specified for parameter 2 Pin
Richard Deeming3-Jul-17 6:29
mveRichard Deeming3-Jul-17 6:29 
GeneralRe: No value specified for parameter 2 Pin
karengsh3-Jul-17 6:33
karengsh3-Jul-17 6:33 
GeneralRe: No value specified for parameter 2 Pin
Richard Deeming3-Jul-17 8:06
mveRichard Deeming3-Jul-17 8:06 
GeneralRe: No value specified for parameter 2 Pin
karengsh3-Jul-17 17:33
karengsh3-Jul-17 17:33 
GeneralRe: No value specified for parameter 2 Pin
Richard MacCutchan3-Jul-17 22:20
mveRichard MacCutchan3-Jul-17 22:20 
GeneralRe: No value specified for parameter 2 Pin
karengsh4-Jul-17 6:51
karengsh4-Jul-17 6:51 
Ok. Tks for the tip. I think now I got a bigger problem which I think it is due to my code and there is no answer at all. I really need help for this one.

Cos now the error is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException : Column 'tutorIC' cannot be null

After thinking of a long time, I think it should be because of the 2nd prepared Statement there is no controller so it becomes null.

I hope to know for my case, how to avoid the above since I have to repeat the similar prepared Statement in my generating keys Ids ?

Java
public void insertTutor(tutor m) throws MyDataException { //
		openConnection();
		try {
			connection.setAutoCommit(false);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String qry = INSERT_QRY;
		try (

				PreparedStatement ps = connection.prepareStatement(qry)) {
			;
			// ps = connection.prepareStatement(qry,
			// Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, m.getName());
			ps.setString(2, m.getNRIC());
			ps.setString(3, m.getEmail());
			ps.setString(4, m.getAddress1());
			ps.setString(5, m.getAddress2());
			ps.setString(6, m.getZipcode());
			ps.setString(7, m.getContactNo());
			ps.setInt(8, m.getAge());
			ps.setString(9, m.getGender());
			blah blah blah
			ps.executeUpdate();
			connection.commit();
			// ResultSet tableKeys = ps.getGeneratedKeys();
			// while(tableKeys.next())
			// tableKeys.next();
			// int tutor_id = tableKeys.getInt(1);
			// m.setTutor_id(tutor_id); // I can't do it like that cos I need my controller to be able to read the tutor_id later
			// System.out.println(tutor_id);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new MyDataException("DB Error");
		} finally {
			if (ps != null) {
				closeConnection();
			}
		}
	}

	public static int writeTutorId() throws Exception {
		openConnection();
		String qry = INSERT_QRY;
		tutor m = new tutor();
		int tutor_id = m.getTutor_id();
		
		PreparedStatement pstmt = connection.prepareStatement(qry, new String[] { "tutor_id" });	
		pstmt.setInt(1, m.getTutor_id());
		pstmt.setString(2, m.getName());
		pstmt.setString(3, m.getNRIC());
		pstmt.setString(4, m.getEmail());
		pstmt.setString(5, m.getAddress2());
		pstmt.setString(6, m.getZipcode());
		pstmt.setString(7, m.getContactNo());		
		pstmt.setInt(8, m.getAge());
		pstmt.setString(9, m.getGender());
		blah blah blah
		if (pstmt.executeUpdate() != -1) {
		//pstmt.executeUpdate();	
			ResultSet rs = pstmt.getGeneratedKeys();
			if (null != rs && rs.next()) {
				tutor_id = rs.getInt(1);
			}
			pstmt.close();
			connection.close();
			
		
	}
		return tutor_id;
}

<pre lang="java">

My 2nd method in which it returns the generated key ids will be used in another insertion for my 3rd table and so I would still need this method to be in.

My controller :
<pre lang="java">
protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		Statement stmt = null;
		tutor m4 = new tutor();		
		manager mgr = new manager();	
		try {
			String name = request.getParameter("name");
			if (name != null){			
			m4.setName(name);
			}
			String NRIC = request.getParameter("nric");
			if (NRIC != null){
			m4.setNRIC(NRIC);			
			}
			String email = request.getParameter("email");
			if (email != null){
			m4.setEmail(email);			
			String address1 = request.getParameter("address1");
			if (address1 != null){
			m4.setAddress1(address1);				
			}
			String address2 = request.getParameter("address2");
			if (address2 != null){
			m4.setAddress2(address2);
			}
			String zipcode = request.getParameter("zipcode");
			if (zipcode != null){
			m4.setZipcode(zipcode);
			}
			String contactNo = request.getParameter("contactNo");
			if (contactNo != null){
			m4.setContactNo(contactNo);			
			}
			int age = Integer.parseInt(request.getParameter("age"));
			if (age != 0){
			m4.setAge(age);
			}
			String gender = request.getParameter("Gender");
			if (gender != null){			
			m4.setGender(gender);
			}
			so on and so for...
			}			
			mgr.insertTutor(m4);			
			} }catch (Exception ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();			
				}	
		
			Subject s = new Subject();
			try{
			
			String[] sub = request.getParameterValues("subject");
						
			List<String> subjName = Arrays.asList(sub);
			//System.out.println(subjName);
			mgr.insertSubject(subjName);
			}catch (Exception ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();			
				}
			
			try{
				tutor_id = tutorDAOImpl.writeTutorId(); // problem is I need the writeTutorId() which is the generated Keys
				subj_id = subjectDAOImpl.Idlist();						
				tutor_SubjectDAOImpl.insertTutor_Subject(tutor_id, subj_id);
			}
			 catch (Exception ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();			
				}
	}
	}

GeneralRe: No value specified for parameter 2 Pin
LynxEffect9-Aug-17 10:56
LynxEffect9-Aug-17 10:56 
Questionusing firefox progmatically with java Pin
Member 132904603-Jul-17 3:45
Member 132904603-Jul-17 3:45 
AnswerRe: using firefox progmatically with java Pin
Richard MacCutchan3-Jul-17 4:22
mveRichard MacCutchan3-Jul-17 4:22 
Questionjava.net.SocketException: Software caused connection abort: socket write error Pin
Django_Untaken1-Jul-17 10:57
Django_Untaken1-Jul-17 10:57 
GeneralRe: java.net.SocketException: Software caused connection abort: socket write error Pin
Richard MacCutchan1-Jul-17 21:53
mveRichard MacCutchan1-Jul-17 21:53 
GeneralRe: java.net.SocketException: Software caused connection abort: socket write error Pin
Django_Untaken2-Jul-17 0:39
Django_Untaken2-Jul-17 0:39 
GeneralRe: java.net.SocketException: Software caused connection abort: socket write error Pin
Richard MacCutchan2-Jul-17 2:37
mveRichard MacCutchan2-Jul-17 2:37 
GeneralRe: java.net.SocketException: Software caused connection abort: socket write error Pin
Django_Untaken2-Jul-17 8:33
Django_Untaken2-Jul-17 8:33 
GeneralRe: java.net.SocketException: Software caused connection abort: socket write error Pin
Richard MacCutchan3-Jul-17 4:23
mveRichard MacCutchan3-Jul-17 4:23 
QuestionInput:12-4-78==OUTPUT:12041978 //Input:9-4-78==OUTPUT:09041978 Pin
Member 1327696824-Jun-17 8:52
Member 1327696824-Jun-17 8:52 
AnswerRe: Input:12-4-78==OUTPUT:12041978 //Input:9-4-78==OUTPUT:09041978 Pin
Michael_Davies24-Jun-17 9:42
Michael_Davies24-Jun-17 9:42 
SuggestionRe: Input:12-4-78==OUTPUT:12041978 //Input:9-4-78==OUTPUT:09041978 Pin
Richard Deeming26-Jun-17 1:46
mveRichard Deeming26-Jun-17 1:46 
AnswerRe: Input:12-4-78==OUTPUT:12041978 //Input:9-4-78==OUTPUT:09041978 Pin
jschell28-Jun-17 7:16
jschell28-Jun-17 7:16 
QuestionHow should I go about getting the generated key from one DAOImpl to another DAOImpl ? Pin
karengsh18-Jun-17 6:30
karengsh18-Jun-17 6:30 
AnswerRe: How should I go about getting the generated key from one DAOImpl to another DAOImpl ? Pin
Mike.F.Hewitt18-Jun-17 12:14
Mike.F.Hewitt18-Jun-17 12:14 
GeneralRe: How should I go about getting the generated key from one DAOImpl to another DAOImpl ? Pin
karengsh23-Jun-17 22:34
karengsh23-Jun-17 22:34 
QuestionJava Pin
Member 1326557218-Jun-17 2:38
Member 1326557218-Jun-17 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.