掲示板

Searching for examplecode

11年前 に Johan de jong によって更新されました。

Searching for examplecode

Junior Member 投稿: 41 参加年月日: 12/02/06 最新の投稿
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
11年前 に David H Nebinger によって更新されました。

RE: Searching for examplecode

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
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
11年前 に Hitoshi Ozawa によって更新されました。

RE: Searching for examplecode

Liferay Legend 投稿: 7942 参加年月日: 10/03/24 最新の投稿
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