Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Java Spring Hibernate : How to make MySql group_concat, concat_ws work in Hibernate

I have an application where I am using spring Hibernate and my applicationContext.xml files looks like below. I want to use group_concat and concat_ws in Hibernate queries.


 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation">
   <value>classpath:hibernate.cfg.xml</value>
  </property>
  <property name="configurationClass">
   <value>org.hibernate.cfg.AnnotationConfiguration</value>
  </property>

  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${jdbc.dialect}</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>

 </bean>

Solution

Add a new java file somewhere in your solution. For example I am adding in the below package
com.myApp.util
Class Name : CustomDialect

package com.myApp.util;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.StringType;
public CustomDialect(){
   registerFunction("group_concat", new StandardSQLFunction("group_concat", StringType.INSTANCE));
    registerFunction("concat_ws", new StandardSQLFunction("concat_ws", StringType.INSTANCE));
}

}

Update applicationConctext.xml like below

....
<props>
<prop key="hibernate.dialect">com.myApp.util.CustomDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
....





How to Convert MM/DD/YYYY format to YYYY-MM-DD HH:MM:SS in Java

How to Convert MM/DD/YYYY format Date to DB Format which is YYYY-MM-DD HH:MM:SS  using Java


//Required Imports

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

//Define outputDate variable
Timestamp outputDate = null;
 //input datestring in MM/DD/YYYY format 
String inputDate = "05/16/2013";
try {    
//create a simpledateformat object to read the input date
 SimpleDateFormat fromDate = new SimpleDateFormat("MM/dd/yyyy");
//create one more simpledateformat object to write output date
 SimpleDateFormat toDBFormat = new SimpleDateFormat("yyyy-MM-dd");
//Parse Input Date to Output format as a String
 String reformattedStr = toDBFormat.format(fromDate.parse(inputDate));
//Now Convert the string to Date
 Date parsedDate = toDBFormat.parse(reformattedStr);
//Now finally convert the Date to Timestamp format which is equivalent to MySQL Format
 transactionDate = new java.sql.Timestamp(parsedDate.getTime());
}catch(Exception e) {
  e.printStackTrace();                       
}

Word Wrap in Eclipse IDE

How to do Word Wrap in Eclipse IDE like in NotePad.

Press Ctrl+Shift+F.

This will format the long lines into small ones

If you need a menu item for this try the below link. I never tried the below link.
http://ahtik.com/blog/projects/eclipse-word-wrap/

ClassNotFoundException com.mysql.jdbc.Driver myeclipse indigo Resolved

Open myeclipse indigo
Create New Java Project.
Add a Class file and write the code shown below for connecting to mysql
Download mysql-connector-java-5.1.21.jar file and add it to myeclipse Run Configurations
Keep the jar file in CLASSPATH environment variable
Include in Java build path also by adding External Jar.

After doing all the above steps I see the below error message. Checked lot of forums and searched google for more than 2 days, every step that I did was correct and nothing looks wrong. 
But still I can't connect to mysql.

Error Messages : 
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at database.Connect.ConnectDB(Connect.java:16)

Error Message : 
No Suitable Driver for jdbc:mysql://localhost:3306/test

Program written in myeclipse indigo IDE
  1. package database;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. public class Connect {
  5. public Connection ConnectDB()
  6. {
  7. Connection conn = null;
  8. try
  9. {
  10. String userName = "root";
  11. String password = "planet";
  12. String url = "jdbc:mysql://localhost:3306/test";
  13. Class.forName ("com.mysql.jdbc.Driver").newInstance();
  14. conn = DriverManager.getConnection (url, userName, password);
  15. System.out.println ("Database connection established");
  16. }
  17. catch (Exception e)
  18. {
  19. e.printStackTrace();
  20. }
  21. return (conn);
  22. }
  23. }

Resolution :

After checking lot of articles on the internet, I couldn't figure out what is going wrong in my eclipse.
The mistake I did was, I downloaded mysql-connector-java-5.1.21.zip file from mysql downloads section and setting the PATH to this file and added the same zip file to the external JAR.

To solve my problem I extracted the ZIP file which internally contains mysql-connector-java-5.1.21.jar file. Once we have the real jar file we should use this file every where. This solved my problem.

Hope this helps anyone out there..  If anyone finds this useful please leave a comment




javax.servlet.jsp.JspWriter cannot be resolved to a type

I suddenly got this error message and not sure what went wrong, after searching for sometime I found the below solution which resolved this problem.

Try doing these steps in your eclipse IDE and this should solve your problem

  1. Go to "Project->Properties->Validation".
  2. Click "Configure Workspace Settings...".
  3. Unselect options for JSP Syntax Validator. You need to uncheck both manual and build