Fórum

Searching for examplecode

Johan de jong, modificado 11 Anos atrás.

Searching for examplecode

Junior Member Postagens: 41 Data de Entrada: 06/02/12 Postagens Recentes
I am in desperate search for a piece of code on how to do the following:

1. portlet needs connection to special MySQL database
2. MySQL connection is managed by Glassfish by JNDI settings
3. I "need" a javaclass that runs a query and then gives my back a List or Array (preferable a List) of the found rows/ data

Is there anyone who is willing to share just a small piece of example code to me?

after putting together some examples i ended up with nothing...

package com.test.base;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import javax.naming.*;

public class TestQuery {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	    // Add/generate public getters + setters.
		public void setId(long long1) {
			// TODO Auto-generated method stub
			
		}
		public void setName(String string) {
			// TODO Auto-generated method stub
			
		}
		public void setValue(int int1) {
			// TODO Auto-generated method stub
			
		}


	
	public static void main(String args[]) throws SQLException, NamingException{
        new TestQuery().doGet();
    }
	
	@SuppressWarnings("null")
	public List<testquery> doGet() throws NamingException, SQLException{
	    //PreparedStatement statement = null;
	    ResultSet resultSet = null;
	    List<testquery> doGet = new ArrayList<testquery>();

	    try {
	    	InitialContext ctx = new InitialContext();
	        DataSource ds = (DataSource)ctx.lookup("jdbc/kpnconfig");
	        
	        Connection conn = ds.getConnection();
	        Statement stmt = conn.createStatement();
	       
	        ResultSet rs = stmt.executeQuery("select * from my_table");
	        while (rs.next()) {
	        	TestQuery data = new TestQuery();
	            data.setId(resultSet.getLong("id"));
	            data.setName(resultSet.getString("name"));
	            data.setValue(resultSet.getInt("value"));
	            doGet.add(data);
	        }
	        stmt.close();
	        conn.close();   
	    } finally {

	    }

	    return doGet;
	}


}
</testquery></testquery></testquery>
thumbnail
David H Nebinger, modificado 11 Anos atrás.

RE: Searching for examplecode

Liferay Legend Postagens: 14919 Data de Entrada: 02/09/06 Postagens Recentes
1 and 2 are not a problem and require no code. Just use the JNDI name in portal-ext.properties for the database info and Liferay will use it.

3 should be done with Service Builder. There is already plenty of doco on liferay.com in the forum, wiki, and blogs to help you use SB.
thumbnail
Hitoshi Ozawa, modificado 11 Anos atrás.

RE: Searching for examplecode

Liferay Legend Postagens: 7942 Data de Entrada: 24/03/10 Postagens Recentes
Following up on David's replied.

Set the following in portal-ext.properties.
##
## JDBC
##

#
# Set the JNDI name to lookup the JDBC data source. If none is set,
# then the portal will attempt to create the JDBC data source based on the
# properties prefixed with "jdbc.default.".
#
#jdbc.default.jndi.name=jdbc/LiferayPool

To execute a query to get a list back, just use dynamicQuery found in most impl class.
http://www.liferay.com/community/wiki/-/wiki/Main/Queries+2%3A+DynamicQuery+API