I've created a Java Script Library which has a java class that contains some jdbc code.
It has a method to get values from a database (mysql).
Now i need to access it in the repeat control like <xp:repeat value = ?? >
But i don't find a way to access it there.
If it is a javascript library, the method is accessed as <xp:repeat value="#{javascript:getSQLData()}"
How to achieve it? And is it the right approach to use java in script libraries when we also have a separate application named Java inside the Code Section (below script library in the application view).
My java code is:
package com.db;
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;
public class Db {
Connection con;
Statement st;
ResultSet rs;
public Db(){
this.connect("localhost", "3306", "vijay", "root", "");
try {
this.query("select * from prodet;");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("///////////////query///////////////////////////////////////////");
e.printStackTrace();
}
}
public void connect(String server, String port, String db, String user, String pwd){
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager
.getConnection("jdbc:mysql://localhost:3306/vijay","root", "");
//con=DriverManager.getConnection("\"jdbc:mysql://"+server+":"+port+"/"+db+"\""+","+"\""+user+"\""+","+"\""+pwd+"\"");
st=con.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
List arr = new ArrayList();
@SuppressWarnings("unchecked")
public void query(String q) throws SQLException{
rs=st.executeQuery(q);
while(rs.next()){
arr.add(rs.getString(2));
arr.add(rs.getInt(3));
}
}
public String getData(){
String arra = arr.toString();
return arra;
}
public String doAllDb(){
return this.getData();
}
public static void main(String a[]){
Db d = new Db();
System.out.println(d.getData());
}
}
And the ssjs to access the method is:
importPackage(com.db);
var v = new com.db.Db(); v.doAllDb();
This ssjs is written under Bind data using ssjs.
<xp:repeat id="repeat1" rows="30">
<xp:this.value><![CDATA[#{javascript:importPackage(com.db);
var v = new com.db.Db(); v.doAllDb();}]]> .
When the xpage is previewed, it is blank. Doesn't show any value. But i tested the java code. It is working fine.