Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Habib Zare
How can Query in database table whitout service builder?
February 27, 2013 8:43 AM
Answer

Habib Zare

Rank: Junior Member

Posts: 43

Join Date: October 28, 2012

Recent Posts

There is no entity(there is no service builder) in my portlet but i want to select or insert data in any table of database.

How can i do that?
Subhash Pavuskar
RE: How can Query in database table whitout service builder?
February 27, 2013 9:04 AM
Answer

Subhash Pavuskar

Rank: Regular Member

Posts: 216

Join Date: March 12, 2012

Recent Posts

Hi habib zare ,

You can do this by manual JDBC connection!! but its having lots of disadvantage !!
For Ex:

 1<%@ page import ="java.sql.*" %>
 2<%@ page import ="javax.sql.*" %>
 3<%
 4    String userid=request.getParameter("user");
 5    session.putValue("userid",userid);
 6    String pwd=request.getParameter("pwd");
 7    Class.forName("com.mysql.jdbc.Driver");
 8    java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
 9    Statement st= con.createStatement();
10    ResultSet rs=st.executeQuery("select * from users where user_id='"+userid+"'");
11
12    if(rs.next())
13    {
14        if(rs.getString(2).equals(pwd))
15        {
16            out.println("welcome"+userid);
17        }
18        else
19        {
20            out.println("Invalid password try again");
21        }
22    } 
23%>
Habib Zare
RE: How can Query in database table whitout service builder?
February 27, 2013 9:05 AM
Answer

Habib Zare

Rank: Junior Member

Posts: 43

Join Date: October 28, 2012

Recent Posts

Thanks Subhash.

Can I do this by custom sql or dynamic query?
Subhash Pavuskar
RE: How can Query in database table whitout service builder?
February 27, 2013 9:33 AM
Answer

Subhash Pavuskar

Rank: Regular Member

Posts: 216

Join Date: March 12, 2012

Recent Posts

Habib Zare:

Can I do this by custom sql or dynamic query?


I dint get your question !!
Habib Zare
RE: How can Query in database table whitout service builder?
February 27, 2013 9:41 AM
Answer

Habib Zare

Rank: Junior Member

Posts: 43

Join Date: October 28, 2012

Recent Posts

Subhash Pavuskar:
Habib Zare:

Can I do this by custom sql or dynamic query?


I dint get your question !!


Can i insert to extisting table by custom-sql or dynamic query?(There is no service.)
Subhash Pavuskar
RE: How can Query in database table whitout service builder?
February 27, 2013 10:16 AM
Answer

Subhash Pavuskar

Rank: Regular Member

Posts: 216

Join Date: March 12, 2012

Recent Posts

Hi

Pls have a look on this thread !!
http://www.liferay.com/community/forums/-/message_boards/message/12569596
Hitoshi Ozawa
RE: How can Query in database table whitout service builder?
February 27, 2013 1:17 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7990

Join Date: March 23, 2010

Recent Posts

You can just a method in Impl class corresponding to the database table class. If it's a liferay table, you probably won't have problem.

Example
UserLocalServiceImpl.dynamicQuery()
Habib Zare
RE: How can Query in database table whitout service builder?
February 27, 2013 7:52 PM
Answer

Habib Zare

Rank: Junior Member

Posts: 43

Join Date: October 28, 2012

Recent Posts

Thanks Hitoshi.

but I added the table in database. table is not liferay table.
Hitoshi Ozawa
RE: How can Query in database table whitout service builder?
February 27, 2013 10:15 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7990

Join Date: March 23, 2010

Recent Posts

but I added the table in database. table is not liferay table.


And you don't want to use service builder. So did you code dynamicquery and customquery methods yourself? If you did, you can just call that.
Habib Zare
RE: How can Query in database table whitout service builder?
February 27, 2013 11:08 PM
Answer

Habib Zare

Rank: Junior Member

Posts: 43

Join Date: October 28, 2012

Recent Posts

Yes Hitoshi.

I can call customquery in my finderimpl class when i use servicebuilder like :

 1 public Object findByName( String name)
 2        {
 3
 4
 5        Session session = null;
 6
 7       
 8            session = openSession();
 9
10                String sql = CustomSQLUtil.get("com.habib.findByName");
11
12        SQLQuery q = session.createSQLQuery(sql);
13
14        QueryPos qPos = QueryPos.getInstance(q);
15
16        return q.list(true);
17
18               }


but if there is no servicebuilder ,i can`t.

i can`t use openSession().

i want any example to learn.

thx.
Hitoshi Ozawa
RE: How can Query in database table whitout service builder?
February 28, 2013 5:15 AM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7990

Join Date: March 23, 2010

Recent Posts

but if there is no servicebuilder ,i can`t.

i can`t use openSession().


That's simply because you didn't create a method. Create it.
How else did you think you can call on a function without generating it or creating it yourself.