Unable to set data to textField from tableView - on Mouseclick and Up and Down Arrow keys (H2 Database) - sql

I am a recent user of the h2 database, I need some assistance with the SQL syntax.
I'm able to retrieve data from the h2 dB and set it into JavaFX tableView, On performing the mouseclick or buttonpress action (Up & Down arrows) the intended behaviour is to display the current row of data from the tableView into the textfields, below is the code.
I'm getting the following exception:
Invalid value "1" for parameter "parameterIndex" [90008-193]
I'm certain this exception is due to SQL grammar unique to the H2 database, as the placeholder (' "+slnoField.getText()+" ' ") works fine in other databases. Please could you suggest the correct syntax or a solution. Many thanks.
#FXML
public void UpdateTable(){
data.clear();
try
{
conn = lrconn.getDatabaseConnection();
String sql = "SELECT * from APP_TABLE ;
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next())
{
data.add(new TestPOJO(
rs.getString("SLNO"),
rs.getString("NAME")
));
Table.setItems(data);
}
pst.close();
rs.close();
}
catch(Exception e1)
{
e1.printStackTrace();
}
Table.setOnMouseClicked((MouseEvent me) ->{
try{
conn = lrconn.getDatabaseConnection();
TestPOJO user = (TestPOJO)Table.getSelectionModel().getSelectedItem();
String sql = "SELECT * from APP_TABLE where SLNO =' "+slnoField.getText()+" ' ";
pst = conn.prepareStatement(sql);
pst.setString(1, user.getSLNO());
rs = pst.executeQuery();
while(rs.next()){
slnoField.setText(rs.getString("SLNO"));
nameField.setText(rs.getString("NAME"));
}
rs.close();
pst.close();
}catch(SQLException ex){
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
);
Table.setOnKeyReleased((KeyEvent e) ->{
if(e.getCode() == KeyCode.UP || e.getCode() == KeyCode.DOWN){
try{
TestPOJO user = (TestPOJO)Table.getSelectionModel().getSelectedItem();
String sql = "SELECT * from APP_TABLE where SLNO =' "+slnoField.getText()+" ' ";
pst = conn.prepareStatement(sql);
pst.setString(1, user.getSLNO());
rs = pst.executeQuery();
while(rs.next()){
slnoField.setText(rs.getString("SLNO"));
nameField.setText(rs.getString("NAME"));
catch(IOException | SQLException ex){
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
);
}

You're not using the PreparedStatement correctly. Place ? at locations where you want to insert parameters in the query string. You don't seem to add one of those:
String sql = "SELECT * from APP_TABLE where SLNO = ?";
pst = conn.prepareStatement(sql);
pst.setString(1, user.getSLNO());
rs = pst.executeQuery();

Related

PreparedStatement SetString doesn't work (case Oracle)

I don't have an idea why this method gets an error
public TdPegawai getTdPegawai(String nip) throws Exception {
PreparedStatement ps = null;
try {
TdPegawai tp = new TdPegawai();
sql = "select * "
"from TD_PEGAWAI " +
"where NIP=? ";
ps = connection.prepareStatement(sql);
ps.setString(1, nip);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
// tp.setblablabla();
}
rs.close();
return tp;
} finally {
ConnectionUtil.closePreparedStatement(ps);
}
}
My SQL variable just returns SELECT * FROM TD_PEGAWAI WHERE NIP=?
My nip variable can return a value.
Is there any something wrong with my preparestatement or setstring ?

Ms access autoNumber when retrieving data

can anyone help with retrieving value from ms access db
the problem with auto number, I want to show it in Jtextfield when I click over a record into the table..
I asked before this question, but no one answer me.
Please help me with this problem as soon as posible.
try {
int row = table_BP.getSelectedRow();
String Table_Clicked = table_BP.getModel().getValueAt(row, 0).toString();
String sql = "Select * from Blood_pressure where Patient_Id='" + Table_Clicked + "'";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()) {
try {
String add1 = rs.getString("BP_Test_ID").toString();
txt_testNo.setText(add1);
String add2 = rs.getString("Patient_Id");
txt_PID.setText(add2);
String add3 = rs.getString("Date_Of_test");
DC_Date.setDateFormatString(add3);
String add4 = rs.getString("Blood_presure");
txt_BP.setText(add4);
String add5 = rs.getString("Remarks");
TA_Remarks.setText(add5);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "sdsdsd");
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "nnnnnnnnnnnnnnnnnnnn");
}

netbeans sql's statment using Like operator

I am trying to pass value from Jcombobox and I used Like Operator in order to retrieve data from Database ..
I need to retrieve all data related to a chosen month from combobox
String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE '?%'";
try {
ps = con.prepareStatement(sql);
ps.setString(1, cmb.getSelectedItem().toString());
rs = ps.executeQuery();
table_PatuentReg.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE '"+cmb.getText().toString()+"%'";
Use this format.
this will help more...
You need to change the query and add the % in the prepared statement value where we are setting the value for string.
String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE ?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, cmb.getSelectedItem().toString()+"?");
rs = ps.executeQuery();
table_PatuentReg.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
Try putting it directly in the ? mark sign, like so
String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE '"+cmb.getSelectedItem().toString()+"%'";
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
table_PatuentReg.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}

Insert data in to oracle table using netbeans

I want to insert data into my oracle table using netbeans text fields and there is a problem with this code. When this screen execute it gives me an exception:
java.sql.SQLException:Invalid column index
Please help me as soon as possible.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
conn = javadb.ConnectDb();
try{
String sql = "insert into addbook (id, title, isbn, author)"
+ " values(null,'','','');";
pst = (OraclePreparedStatement) conn.prepareStatement(sql);
pst.setString(1, title.getText());
pst.setString(2, isbn.getText());
pst.setString(3, author.getText());
rs = (OracleResultSet) pst.executeQuery();
}catch(SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null, e);
}
}
conn = JavaDbCon.ConnecrDb();
try{
String sql = "insert into Balance (ecode,ltype,rol,ldate) values(?,?,?,?)";" // there he show me error
pst = (OraclePreparedStatement) conn.prepareStatement(sql);
pst.setString(1,'NULL'); // also here
pst.setString(2, ltype.getText());
pst.setString(3, rol.getText());
pst.setString(4, ldate.getText());
rs = (OracleResultSet) pst.executeQuery();
}catch(SQLException | HeadlessException e){ // here also
JOptionPane.showMessageDialog(null, e);
}
This should work, try this.
conn = javadb.ConnectDb();
try{
String sql = "insert into addbook (id, title, isbn, author) values(?,?,?,?)";
pst = (OraclePreparedStatement) conn.prepareStatement(sql);
pst.setString(1,'NULL');
pst.setString(2, title.getText());
pst.setString(3, isbn.getText());
pst.setString(4, author.getText());
rs = (OracleResultSet) pst.executeUpdate(sql);
}catch(SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null, e);
}
}

Can anyone see why this SQL insert will not work? (Exception to String and Date to String Inserted) - JDBC

Im trying to basically make a log of errors generated from a Java program and save them in an SQL table with 2 colums( Error and Date )
Try and catch sends the error to the method which should hopefully insert into database but its failing somewhere. Im new enough to Java so im not sure how to debug it properly or get more details on wahts going wrong and where
Heres the method.
public void createErrorLog(Exception error) throws SQLException{
// Error as String
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
error.printStackTrace(pw);
sw.toString(); // stack trace as a string
String errorOne = sw.toString();
System.out.println(errorOne);
// Date
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
String dateString = dateFormat.format(date);
Connection conn = null;
try {
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
System.out.println("Connected to database.");
String insertSaleProperty = "INSERT INTO ErrorLogs"
+ "(Error, "
+ "Time, "
+ "(?,?)";
PreparedStatement preparedStatement = conn.prepareStatement(insertSaleProperty);
preparedStatement.setString(1, errorOne);
preparedStatement.setString(2, dateString);
// execute insert SQL statement
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.err.println("Cannot write to database.");
} finally {
if(conn != null){
conn.close();
}
}
}
Try the following.
String insertSaleProperty = "INSERT INTO ErrorLogs"
+ "(Error,Time) values "
+ "(?,?)";
String insertSaleProperty = "INSERT INTO ErrorLogs"
+ "(Error, "
+ "Time, "
+ "(?,?)";
Should probably be:
String insertSaleProperty = "INSERT INTO ErrorLogs "
+ "(Error, Time) "
+ "VALUES (?,?)";