Friday, November 30, 2012

How to convert String to Java.sql.date and Java.sql.time

 DateFormat DOB = new SimpleDateFormat("yyyy-MM-dd");
    String date ="\"1234-01-01\"";
    java.sql.Date convertedDate=null;
    try {
        convertedDate = new java.sql.Date(DOB.parse(date.replaceAll("\"","")).getTime());
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
    System.out.println(convertedDate);

split a String by * in java

Let say we have a String something like below.

String sample="abc*123";

We want to split this String by '*'. We can't do this directly.Because in split the * has a special meaning.

This not working,
String spl[]=sample.split("*");

Therefore, we should use something like below.

String spl[]=sample.split("\\*");

execute batch of queries in one time using java and JDBC

Let say we have something like below.

String [] queries = {
    "insert into employee (name, city, phone) values ('A', 'X', '123')",
    "insert into employee (name, city, phone) values ('B', 'Y', '234')",
    "insert into employee (name, city, phone) values ('C', 'Z', '345')",
};
             
Connection connection = new getConnection();
Statement statemenet = connection.createStatement();
             
for (String query : queries) {
    statemenet.execute(query); //execute query one by one
}
statemenet.close();
connection.close();
 
Execute all the queries one by one.This is not a good practice.We can use something like below.Execute all the queries in once.
 
 
 
Connection connection = new getConnection();
Statement statemenet = connection.createStatement();
 
for (String query : queries) {
    statemenet.addBatch(query);
}
statemenet.executeBatch();
statemenet.close();
connection.close();
 
 
 
 
As well as we can avoid sql injection by using something like below.
 
String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
 
final int batchSize = 1000;
int count = 0;
 
for (Employee employee: employees) {
 
    ps.setString(1, employee.getName());
    ps.setString(2, employee.getCity());
    ps.setString(3, employee.getPhone());
    ps.addBatch();
     
    if(++count % batchSize == 0) {
        ps.executeBatch();
    }
}
ps.executeBatch(); // insert remaining records
ps.close();
connection.close();
 
 
 

Tuesday, November 20, 2012

Files on flash drive changed to shortcuts fix

If you did not format your flash drive, then check whether the files are in hidden mode. Then follow these steps:
  • Click on the link below and download the file "AutorunExterminator":
  • Extract it: Double-click on "AutorunExterminator"
  • Plug your pendrive now
    • This will remove the autorun.inf files from your pendrive and also from the drives
  • Click on Start > Run, type cmd and click OK
  • Here it is assumed your pendrive letter is G (change as necessary)
  • Enter this command.
    • attrib -h -r -s /s /d g:\*.*      
  • Press Enter
  • Now check for your files in on the pen drive
  • Now download the Malwarebytes' Anti-Malware from this link:http://en.kioskea.net/download/download-105-malwarebytes-anti-malware
  • Update it and perform a "Full Scan"
    • Note : The default selected option is "Quick Scan"

Thursday, November 8, 2012

Configure CAS SSO with mysql database

When i try to configure mysql with CAS There were lots of problem occurred and i cannot find a good tutorials about this.I followed some tutorials but i failed.Finally i found the way.So i decided to write a post about it.I explain this in localhost


Prerequest

Install mysql on the machine and be sure it's working properly.I used the default settings of mysql.If you used different one, change the configuration according to it.(localhost:3306)

Download apache tomcat and configure it to allow SSL connection.( You can follow my previous post)

Download cas server source code package

Install apache maven


How


  • Go to CAS source code location -> cas-server-webapp ->src->main->webapp->WEB-INF



  • Open deployerConfigContext.xml in a text editor



  • Change the bean serviceRegistryDao in deployerConfigContext.xml to something like this



<bean id="serviceRegistryDao" class="org.jasig.cas.services.JpaServiceRegistryDaoImpl"
   p:entityManagerFactory-ref="entityManagerFactory" />
 
<!-- This is the EntityManagerFactory configuration for Hibernate -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true"/>
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
 
    <tx:annotation-driven transaction-manager="transactionManager"/>
 
   


<!-- change test as your database name, and give username and password of mysql login credential-->
<bean
        id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/test?autoReconnect=true"
        p:password=""
        p:username="root" />






  • Change following line



<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>







  • Open cas.properties file


database.hibernate.dialect=org.hibernate.dialect.MySQLDialect





  • Go to CAS source code location -> cas-server-webapp
  • Open pom.xml and add following dependency according to your CAS version

<!--
    Apache Commons DBCP
      for Java 6 (use version 1.3 for Java 5 or lower)
-->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
    <scope>runtime</scope>
</dependency>
 
<!--
    Hibernate Core and Entity Manager
      for CAS 3.5.0 and higher
-->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <scope>compile</scope>
</dependency>
 
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
</dependency>
 
<!--
    Hibernate Entity Manager
      for CAS 3.4.2.1
-->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.5.0-CR-2</version>
</dependency>
 
<!-- 
    MySQL Connector
-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.20</version>
</dependency>