0

I have a JSF webapp and I encountered one problem. I want to take data that is from one table and is inserted in a datatable and insert it into a log table in the database.

I did the connection with the database but it doesn't show any errors and it doesn't insert anything.

I went in with the debugger from netbeans and the variables that I want to insert have values. I do not know where the problem is.

Here is the code:

package com.spv.raport;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.sql.DataSource;
import oracle.jdbc.pool.OracleDataSource;

@Named(value = "dateBean")
@RequestScoped
public class DateBean {

private List<Date> dateList = new ArrayList<>();

public DateBean() throws ClassNotFoundException, SQLException {

    String host = "jdbc:oracle:thin:@xxx:1521:xxx";
    String user = "xxx";
    String pass = "xxx";

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con = DriverManager.getConnection(host, user, pass);
    Statement stmt = con.createStatement();
    String sql = "select utilizator, tip_cerere, parametri, stare from 
    cereri where tip_cerere='D394' and utilizator='44192556' ";
    ResultSet rs = stmt.executeQuery(sql);

    String u = null;
    String t = null;
    String p = null;
    String s = null;
    String d = null;

    while(rs.next()) {

         u = rs.getString("utilizator");
         t = rs.getString("tip_cerere");
         p = rs.getString("parametri");
         s = rs.getString("stare");
        // d = rs.getString("creation_date");

    dateList.add(new Date(u,t,p,s));
    }

    String host2 = "jdbc:oracle:thin:@xxx:xxx";
    String user2 = "xxx";
    String pass2 = "xxx";
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con2 = DriverManager.getConnection(host2, user2, pass2);
    Statement stmt2 = con2.createStatement();
    String sql2 = "insert into SPV_RAPORT_LOG values ('serban', '123', 
    'parametru_test')";

    stmt2.executeUpdate(sql);
    } catch(SQLException se) {System.out.println(se);}
      catch(Exception e) {System.out.println(e);}      
}

public List<Date> getDateList() {
    return dateList;
}

}

The database host and name and user and passwords are put intentionally with xxx to hide them.

I am a beginner and I do not have any idea why it doesn`t insert the test statement into the database table

APC
  • 144,005
  • 19
  • 170
  • 281
S C
  • 73
  • 7
  • Please start with another tutorial, you sort of violate all rules of good development. And start with a unittest. 99,9% of the questions tagged both jsf and something database related (sql, jdbc, oracle) not related to the combination but to either jsf or database things. Narrow the problem down and read all >25 upvoted Q/A on JSF for good code development – Kukeltje Mar 12 '19 at 09:29
  • I started working in programming for two months...I am a beginner/junior – S C Mar 12 '19 at 09:32
  • 1
    That is not a problem. Great you started doing this, but even more reason to take a step back and start with some basics about good software development. Believe me, it will help you in the future. E.g. start with https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao and for database things, drop pure jdbc and use JPA – Kukeltje Mar 12 '19 at 09:36
  • And please read [mcve] and imprpve the question – Kukeltje Mar 12 '19 at 20:55
  • I resolved the problem and I will read what you said, thanks for the advice. – S C Mar 13 '19 at 07:54

0 Answers0