Fóruns

Início » Liferay Portal » English » 3. Development

Visualização combinada Visão plana Exibição em árvore
Tópicos [ Anterior | Próximo ]
toggle
Johan de jong
Searching for examplecode
20 de Julho de 2012 14:04
Resposta

Johan de jong

Ranking: Junior Member

Mensagens: 35

Data de entrada: 6 de Fevereiro de 2012

Mensagens 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...
 1
 2package com.test.base;
 3import java.sql.Connection;
 4import java.sql.SQLException;
 5import java.sql.Statement;
 6import java.sql.ResultSet;
 7import java.util.ArrayList;
 8import java.util.List;
 9
10import javax.sql.DataSource;
11
12import javax.naming.*;
13
14public class TestQuery {
15   
16    /**
17     *
18     */
19    private static final long serialVersionUID = 1L;
20
21        // Add/generate public getters + setters.
22        public void setId(long long1) {
23            // TODO Auto-generated method stub
24            
25        }
26        public void setName(String string) {
27            // TODO Auto-generated method stub
28            
29        }
30        public void setValue(int int1) {
31            // TODO Auto-generated method stub
32            
33        }
34
35
36   
37    public static void main(String args[]) throws SQLException, NamingException{
38        new TestQuery().doGet();
39    }
40   
41    @SuppressWarnings("null")
42    public List<TestQuery> doGet() throws NamingException, SQLException{
43        //PreparedStatement statement = null;
44        ResultSet resultSet = null;
45        List<TestQuery> doGet = new ArrayList<TestQuery>();
46
47        try {
48            InitialContext ctx = new InitialContext();
49            DataSource ds = (DataSource)ctx.lookup("jdbc/kpnconfig");
50            
51            Connection conn = ds.getConnection();
52            Statement stmt = conn.createStatement();
53          
54            ResultSet rs = stmt.executeQuery("select * from my_table");
55            while (rs.next()) {
56                TestQuery data = new TestQuery();
57                data.setId(resultSet.getLong("id"));
58                data.setName(resultSet.getString("name"));
59                data.setValue(resultSet.getInt("value"));
60                doGet.add(data);
61            }
62            stmt.close();
63            conn.close();   
64        } finally {
65
66        }
67
68        return doGet;
69    }
70
71
72}
David H Nebinger
RE: Searching for examplecode
20 de Julho de 2012 16:07
Resposta

David H Nebinger

Ranking: Liferay Legend

Mensagens: 7008

Data de entrada: 1 de Setembro de 2006

Mensagens 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.
Hitoshi Ozawa
RE: Searching for examplecode
20 de Julho de 2012 18:47
Resposta

Hitoshi Ozawa

Ranking: Liferay Legend

Mensagens: 7990

Data de entrada: 23 de Março de 2010

Mensagens 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