0

I wrote a java class to get values from a Mysql db in an Xpage. The code is,

package com.vijay;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {

    String name;
    String caty;
    float price;
    private Connection con;
    private Statement st;
    private ResultSet rs;

    public String test(){
        return "This is a class with just a single method";
    }

    public Test(){
    }

    public void db(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/vijay","root","");
            st=con.createStatement();
            st.executeQuery("select * from prodet;");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.getMethod();
    }

    public void getMethod(){
            try {
                while(rs.next()){
                    name=rs.getString("name");
                    price=rs.getFloat("price");
                    caty = rs.getString("caty");                
                }
                con.close();
                st.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

I put a label in the page and Computed its value with SSJS

var v = new com.vijay.Test();
v.db();
v.getMethod();
return v.name;

Even i put those two methods in the constructor too. But doesn't work.

Where do i miss?

benka
  • 4,732
  • 35
  • 47
  • 58
VijayaRagavan
  • 221
  • 2
  • 15
  • Does your ResultSet contain any results? That is, does your rs.next() ever return true? Another thing, if you wrap your SSJS code into a try..catch block, do you get any error messages? – Lauri Laanti Feb 14 '14 at 07:08
  • Ya it returned the statement which i gave in the catch block (in ssjs). – VijayaRagavan Feb 14 '14 at 08:50
  • I did miss this pal: assigning the value st.execute() to the result set object. It should be rs= st.execute(query); – VijayaRagavan Feb 14 '14 at 08:52

1 Answers1

0

I think you must declare your field name as public.

public class Test {
   public String name;

Or much better, create a getter function:

public class Test {
   private String name;
   public String getName() {
     return name;
   }

And then change your SSJS code to return v.getName().

By the way, there are two datasources in Extension Library (JDBCRowSet and JDBCQuery) which might be of interest to you.

Lauri Laanti
  • 948
  • 7
  • 11