IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » java The latest public java snipts.

showing 1-20 of 87 snipts for java
  • NullPointer check - Intellij Live Template
    if($1$ == null) {
        throw new NullPointerException("$1$");
    }
    

    copy | embed

    0 comments - tagged in  posted by chilicat on Sep 02, 2010 at 4:19 p.m. EDT
  • a valid java program
    \u0070\u0075\u0062\u006c\u0069\u0063 \u0063\u006c\u0061\u0073\u0073 \u0055\u0067\u006c\u0079
    
    \u007b
      \u0070\u0075\u0062\u006c\u0069\u0063 \u0073\u0074\u0061\u0074\u0069\u0063 \u0076\u006f\u0069\u0064 \u006d\u0061\u0069\u006e\u0028
    
      \u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d \u0061\u0072\u0067\u0073\u0029
      \u007b
    
        \u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074
    
        \u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0022\u0048\u0065\u006c\u006c\u006f\u0020\u0077\u0022 \u002b
    
        \u0022\u006f\u0072\u006c\u0064\u0022\u0029\u003b
      \u007d
    \u007d
    

    copy | embed

    0 comments - tagged in  posted by OutOfBrain on Aug 20, 2010 at 5:41 p.m. EDT
  • strage java behavious - unicode, error in comments
    // \u
    

    copy | embed

    0 comments - tagged in  posted by OutOfBrain on Aug 19, 2010 at 12:46 p.m. EDT
  • java down to; for trick
    for (int i = 20; i --> 0;) // "20 down to 0"
    {
    //...
    }
    

    copy | embed

    0 comments - tagged in  posted by OutOfBrain on Aug 18, 2010 at 4:46 p.m. EDT
  • java confuse flow
        try
        {
          return;
        }
        finally
        {
          System.out.println("Bye!");
        }
    

    copy | embed

    0 comments - tagged in  posted by OutOfBrain on Aug 11, 2010 at 5:35 p.m. EDT
  • java anonymous init list
        new JFrame()
        {
          {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.add(new JPanel()
            {
              {
                this.add(new JLabel("just a label")
                {
                  {
                    this.setForeground(Color.RED);
                    this.setBackground(Color.BLACK);
                  }
                });
              }
            });
            this.setSize(200, 200);
            this.setVisible(true);
          }
        };
    

    copy | embed

    0 comments - tagged in  posted by OutOfBrain on Aug 11, 2010 at 5:04 p.m. EDT
  • java double foreach
        int[] i = { 1, 2, 3 };
        String[] s = { "A", "B", "C" };
        for (int _i : i)
        {
          for (String _s : s)
          {
            System.out.println(_s + _i);
          }
        }
    

    copy | embed

    0 comments - tagged in  posted by OutOfBrain on Aug 11, 2010 at 5:01 p.m. EDT
  • secuencia hibernate
    public String getSeqEventoEspecial() throws BusinessException {
    		String secuencia = "";
    		StringBuilder qry = new StringBuilder();
    		
    		try {
    			Session sess = ht.getSessionFactory().openSession();
    			secuencia =  sess.createSQLQuery("select SEQ_EVENTOESP.nextval from dual").list().get(0).toString();
    			System.out.println(secuencia);
    			sess.close();
    		} catch (Exception e) {
    			log.error("Error al obtener la secuencia: ",e);
    			e.printStackTrace();
    		}
    		
    
    		return secuencia;
    	}
    

    copy | embed

    0 comments - tagged in  posted by ezhgnu on Aug 05, 2010 at 6:32 p.m. EDT
  • sample hibernate entity hbm
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    
        <class name="com.ssh.tzx.pojos.Article" table="t_article">
            <id name="id">
                <generator class="native"/>
            </id>
            <property name="username"/>
            <property name="title"/>
            <property name="content" type="org.springframework.orm.hibernate3.support.ClobStringType" update="true"
                insert="true"></property>
            <property name="articledate"></property>
            <property name="browseHistoryCount" />
            <many-to-one name="articleCat" column="articleCatId" lazy="false"/>
        </class>
    
    </hibernate-mapping>
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 9:05 a.m. EDT
  • sample spring hibernate integration config
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	     xmlns:aop="http://www.springframework.org/schema/aop"
    	     xmlns:tx="http://www.springframework.org/schema/tx"
    	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     	<property name="configLocation">
     		<value>classpath:hibernate.cfg.xml</value>
     	</property>
     	<property name="lobHandler">
       		<ref bean="lobHandler" />
       	</property>
     </bean>
     
     <bean name="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="default" />
     
     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
     	<property name="sessionFactory">
     		<ref bean="sessionFactory"/>
     	</property>
     </bean>
     
     <tx:advice id="advice" transaction-manager="transactionManager" >
     	<tx:attributes>
     		<tx:method name="add*" propagation="REQUIRED"/>
     		<tx:method name="dele*" propagation="REQUIRED"/>
     		<tx:method name="modify*" propagation="REQUIRED"/>
     		<tx:method name="find*" propagation="REQUIRED"/>
     		<tx:method name="save*" propagation="REQUIRED"/>
     		<tx:method name="init*" propagation="REQUIRED"/>
     		<tx:method name="jump*" propagation="REQUIRED"/>
     		<tx:method name="refresh*" propagation="REQUIRED"/>
     		<tx:method name="*" read-only="true"/>
     	</tx:attributes>
     </tx:advice>
     
     <aop:config>
     	<aop:pointcut id="managerMethod" expression="execution(* com.ssh.tzx.daoimpl.*.*(..))"/>
     	<aop:advisor pointcut-ref="managerMethod" advice-ref="advice"/>
     </aop:config>
    
    </beans>
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 9:03 a.m. EDT
  • empty ehcache config file
    <ehcache>
        <!-- Sets the path to the directory where cache .data files are created.
             If the path is a Java System Property it is replaced by
             its value in the running VM.
             The following properties are translated:
             user.home - User's home directory
             user.dir - User's current working directory
             java.io.tmpdir - Default temp file path -->
        <diskStore path="java.io.tmpdir"/>
    
        <!--Default Cache configuration. These will applied to caches programmatically created through
            the CacheManager.
            The following attributes are required for defaultCache:
            maxInMemory       - Sets the maximum number of objects that will be created in memory
            eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element is never expired.
            timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                                if the element is not eternal. Idle time is now - last accessed time
            timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                                if the element is not eternal. TTL is now - creation time
            overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                                has reached the maxInMemory limit.
            -->
        <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            />
    </ehcache>
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 9:00 a.m. EDT
  • struts2 startup actions descriptions config
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
        <package name="test" namespace="/example" extends="struts-default">
        	<action name="test" class="com.buaaer.struts2demo.DemoAction">
        		<result name="success">/WEB-INF/pages/testSuccess.jsp</result> 
        	</action>
        </package>
    </struts>
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 8:58 a.m. EDT
  • hibernate configuration startup
    # connection info
    hibernate.connection.url=jdbc:mysql://localhost/eshop
    hibernate.connection.driver_class=com.mysql.jdbc.Driver
    hibernate.connection.username=root
    hibernate.connection.password=
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
    
    # hibernate configuration parameters
    hibernate.hbm2ddl.auto=update
    hibernate.show_sql=true
    hibernate.cache.use_query_cache=true
    hibernate.jdbc.batch_size=20
    hibernate.current_session_context_class=thread
    
    # second level cache configuration
    hibernate.cache.use_second_level_cache=true
    hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 8:56 a.m. EDT
  • hibernate sessionfactory startup config
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    	<session-factory>
    		<mapping resource="com/ssh/tzx/pojos/Article.hbm.xml" />
    		<mapping resource="com/ssh/tzx/pojos/Author.hbm.xml" />
    		<mapping resource="com/ssh/tzx/pojos/Category.hbm.xml" />
    	</session-factory>
    </hibernate-configuration>
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 8:52 a.m. EDT
  • log4j sample configuration
    ### direct log messages to stdout ###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    ### direct messages to file hibernate.log ###
    #log4j.appender.file=org.apache.log4j.FileAppender
    #log4j.appender.file.File=hibernate.log
    #log4j.appender.file.layout=org.apache.log4j.PatternLayout
    #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    ### set log levels - for more verbose logging change 'info' to 'debug' ###
    
    log4j.rootLogger=warn, stdout
    
    #log4j.logger.org.hibernate=info
    #log4j.logger.org.hibernate=debug
    
    ### log HQL query parser activity
    #log4j.logger.org.hibernate.hql.ast.AST=debug
    
    ### log just the SQL
    #log4j.logger.org.hibernate.SQL=debug
    
    ### log JDBC bind parameters ###
    #log4j.logger.org.hibernate.type=info
    #log4j.logger.org.hibernate.type=debug
    
    ### log schema export/update ###
    #log4j.logger.org.hibernate.tool.hbm2ddl=debug
    
    ### log HQL parse trees
    #log4j.logger.org.hibernate.hql=debug
    
    ### log cache activity ###
    #log4j.logger.org.hibernate.cache=debug
    
    ### log transaction activity
    #log4j.logger.org.hibernate.transaction=debug
    
    ### log JDBC resource acquisition
    #log4j.logger.org.hibernate.jdbc=debug
    
    ### enable the following line if you want to track down connection ###
    ### leakages when using DriverManagerConnectionProvider ###
    #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 8:46 a.m. EDT
  • struts2 startup config
    <filter>
    	<filter-name>struts2</filter-name>
    	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
    	<filter-name>struts2</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 8:45 a.m. EDT
  • java web-app version 2.4 template
    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app version="2.4" 
    
      xmlns="http://java.sun.com/xml/ns/j2ee" 
    
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    
    
      <welcome-file-list>
    
        <welcome-file>index.jsp</welcome-file>
    
      </welcome-file-list>
    
      <error-page>   
    
        <error-code>404</error-code>   
    
        <location>/noFound.html</location>   
    
      </error-page>   
    
      <error-page>   
    
        <error-code>500</error-code>   
    
        <location>/error.html</location>   
    
      </error-page>
    
    </web-app>
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 8:44 a.m. EDT
  • spring startup config in web.xml
    <context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>
    		classpath*:applicationContext-*.xml
    	</param-value>
    </context-param>
    
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 8:33 a.m. EDT
  • open session in view in spring hibernate integration
    <filter>
    	<filter-name>OpenSessionInViewFilter</filter-name>
    	<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    
    <filter-mapping>
    	<filter-name>OpenSessionInViewFilter</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>
    

    copy | embed

    0 comments - tagged in  posted by Titan on Jul 20, 2010 at 8:31 a.m. EDT
  • calculadora winder
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaapplication1;
    
    import javax.swing.JOptionPane;
    
       
    /**
     *
     * @author Hipolito
     */
    public class Main {
    
           public static float Adicao(float x, float y)
            {
               float resultado;
               resultado=x+y;
              JOptionPane.showMessageDialog(null,resultado);
    
               return 0;
            }
    
          public static float Subtracao(float x,float y)
            {
              float resultado;
              resultado= x-y;
             JOptionPane.showMessageDialog(null,resultado);
              return 0;
          }
    
            public static float divisao (float x,float y)
            {
                float resultado;
              resultado= x/y;
            JOptionPane.showMessageDialog(null,resultado);
              return 0;
            }
    
            public static float  Multiplicacao(float x,float y)
            {
                float resultado;
              resultado= x*y;
             JOptionPane.showMessageDialog(null,resultado);
              return 0;
            }
        
        /**
         * @param args the command line arguments
         */
    
            public static void main(String[] args) {
     int decidirRepeticao;
                do{
    
    
            float a,b;
       int escolha;
         
               
         JOptionPane.showMessageDialog(null,"\t********** Calculadora do Winder ***********");
        JOptionPane.showMessageDialog(null,"------- Digite a operacao matematica que voce quer fazer \n");
        JOptionPane.showMessageDialog(null,"1---Adicao \n");
        JOptionPane.showMessageDialog(null,"2---Subtracao\n");
        JOptionPane.showMessageDialog(null,"3---multiplicacao\n");
         JOptionPane.showMessageDialog(null,"4---divisao\n");
                escolha=Integer.parseInt(JOptionPane.showInputDialog(null,"Escolha uma operacao ","\t",3));
         if(escolha!=1&&escolha!=2&&escolha!=3&&escolha!=4&&escolha!=5)
         {
             JOptionPane.showMessageDialog(null,"Digite o numero 1,2,3,4 ou 5 \n\n");
                     System.exit(0); 
        
        }
              switch(escolha)//funcao switch declarada
        {
                      case 1:
                       JOptionPane.showMessageDialog(null,"\n Digite o primeiro numero e tecle enter, digite o segundo e tecle enter\n");
                      break;
                      case 2:
                      JOptionPane.showMessageDialog(null,"\n Digite o primeiro numero e tecle enter, digite o segundo e tecle enter \n");
                      break;
                      case 3:
                      JOptionPane.showMessageDialog(null,"\n Digite o primeiro numero e tecle enter, digite o segundo e tecle enter \n");
                      break;
                      case 4:
                      JOptionPane.showMessageDialog(null,"\n Digite o primeiro numero e tecle enter, digite o segundo e tecle enter \n");
                      break;
                      case 5:
                      System.exit(0);
       }//fim de switch
                      a=Float.parseFloat(JOptionPane.showInputDialog(null,"\t ","\t",3));
         b=Float.parseFloat(JOptionPane.showInputDialog(null,"\t ","\t",3));
         if(escolha==1)
                 {
        Adicao(a,b);
                 }
        if(escolha==2)
                 {
                 Subtracao(a,b);
                 }
        if(escolha==3)
                {
                 Multiplicacao(a,b);
                }
                if(escolha==4)
                 {
                 divisao(a,b);
                 } 
    
            decidirRepeticao=Integer.parseInt(JOptionPane.showInputDialog(null,"Para reiniciar a calculadora, digite 1, para sair digite qualquer outro numero\n ","\t",3));
     }while(decidirRepeticao==1);
         System.exit(0);
         // TODO code application logic here
    
    
    
        }}
    

    copy | embed

    0 comments - tagged in  posted by winderdias on May 25, 2010 at 2:54 p.m. EDT
Sign up to create your own snipts, or login.