blogjava's web log

blogjava's web log
...

初学者有用.整理一些初级代码


连结mysql
try
{
  Class.forName(
"org.gjt.mm.mysql.Driver").newInstance();

}

catch(Exception e1)
{
  out.print(
"11111111111111111111"+e1.getMessage());
}

try
{
   String url
="jdbc:mysql://localhost/test";
  java.sql.Connection cn
=java.sql.DriverManager.getConnection(url,"root","wujun");
 
// Connection cn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=wujun&useUnicode=true&characterEncoding=8859_1");
  java.sql.Statement sm=cn.createStatement();
  String strsql
="select * from table1";
  java.sql.ResultSet rs
=sm.executeQuery(strsql);
  
while(rs.next())
  
{
    out.print(rs.getInt(
1)+":");
    out.print(rs.getString(
2));
    out.print(
"********************<br>");
  }

  
//rs.close();
  sm.close();
  cn.close();
}


catch(Exception ee)
{
  out.print(ee.getMessage());
}

#########################################################################


Properties 文件操作

 java.util.Properties prp=new Properties();
    java.io.FileInputStream ipt
=new FileInputStream("c:/1.properties");
    prp.load(ipt);
    java.util.Enumeration en
=prp.keys();
    
while(en.hasMoreElements())
    
{
      out.print(en.nextElement());
      
//out.print(prp.get(en.nextElement()));
    }

    
//prp.put("name","wujunjun");
    
//prp.put("name1","aaaaa");
   
// prp.put("name2","bbbbb");
   
// prp.put("name3","ccccc");
   
// prp.put("name4","dddddd");
  
//  try
  
//  {

     
// java.io.FileOutputStream fot=new FileOutputStream("c:/1.properties");
    
//  prp.store(fot,"first in properties");

    
//}
   
// catch(Exception ee)
   
// {
    
//  out.print(ee.getMessage());
   
// }

#########################################################################
日志测试

package logtest.test;

import org.apache.log4j.*;

import java.io.*;
public class Test {
    
private Logger log=Logger.getLogger(this.getClass());
    
public Test() {
    }

    
public void add()
    
{
       
try
       
{
           
int i=0;
           File f
=new File("c:\\a.txt");
           java.io.FileReader fr
=new FileReader(f);
           fr.toString();
          
this.del();
       }

       
catch(Exception ee){
           log.fatal(
"fatal:"+ee);
           log.error(
"error:"+ee);
           log.warn(
"warn"+ee);
           log.info(
"OK,error begin");
           log.debug(
"debug:"+ee);
       }

    }

    
public void del()
            
{
                
new Exception("djkflsjkfsjdklfjsdlkflsdjkjk");
            }

    
public static void main(String[] args) {
        Test test 
= new Test();
        test.add();
    }

}


class目录下

log4j.properties

log4j.rootLogger=fatal,console,file
log4j.appender.console
=org.apache.log4j.ConsoleAppender
log4j.appender.file
=org.apache.log4j.RollingFileAppender
log4j.appender.file.File
=log.txt

log4j.appender.console.layout
=org.apache.log4j.SimpleLayout
log4j.appender.file.layout
=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern
=%%- %m%%C

########################################################################

集合hashTable

#####################################################################

package hashTable;

import java.util.*;

public class hashTable
{
    
    
public static void main(String args[])
    
{
        
try
        
{
        
        java.util.Hashtable hs
=new Hashtable();
        
        
for(int i=0;i<10;i++)
        
{
            hs.put(i,i
+"a");
        }

        
        Enumeration e2
=hs.keys();
        System.out.println(
"key:");
        
while(e2.hasMoreElements())
        
{
            
            
//System.out.print(e2.nextElement()+"value:");
            System.out.println(hs.get(e2.nextElement()));
        }

        java.util.Enumeration ee
=hs.elements();
        System.out.println(
"value:");
        
while(ee.hasMoreElements())
        
{
            System.out.println(ee.nextElement());
        }

        }

        
catch(Exception e)
        
{
            System.out.println(e.getMessage());
        }

    }

}

#######################################################################
集合Vector
########################################################################

package vector;

import java.util.*;


public  class MyVector extends Vector
{
    
public void addInt(int i)
    
{
        addElement(
new Integer(i));    
    }

    
public void addFloat(Float f)
    
{
        addElement(
new Float(f));
    }

    
public void addString(String str)
    
{
        addElement(str);
    }

    
public void addCharArray(char a[])
    
{
        addElement(a);
    }

    
public void printVector()
    
{
        Object o;
        
int lenght=size();
        System.out.println(
"向量元素数为:"+lenght+";他们是:");
        
for(int i=0;0<lenght;i++)
        
{
            o
=elementAt(i);
            
if(o instanceof char[])
            
{
                System.out.println(String.copyValueOf((
char[])o));
            }

            
else
            
{
                System.out.println(o.toString());
            }

        }

        
    }

    
    
public static void main(String args[])
    
{
        MyVector v
=new MyVector();
        
int digit=5;
        
float feal=3.14f;
        
char letters[]={'a','b','c','f','d'
        }
;
        String s
=new String("挖,你在那里");
        v.addInt(digit);
        v.addFloat(feal);
        v.addString(s);
        v.addCharArray(letters);
        v.printVector();
        
    }

        
}

#######################################################################
                         转载请注名出处:http://www.blogjava.net/wujun
#######################################################################
VectorExcample

package vector;

import java.util.*;

public class VectorExcample
{
    
public static void main(String  agrs[])
    
{
        Vector v
=new Vector(3,2);
        System.out.println(
"大小"+v.size());
        System.out.println(
"容量"+v.capacity());
        v.addElement(
new Integer(2));
        v.addElement(
new Integer(3));
        v.addElement(
new Integer(7));
        v.addElement(
new Integer(5));
        System.out.println(
"当前容量:"+v.capacity());
        v.addElement(
new Double(10.04));
        v.addElement(
new Float(11.4));
        System.out.println(
"当前容量:"+v.capacity());
        
if(v.contains(new Integer(2)))
        
{
            System.out.println(
"向量包括2");
            }

        java.util.Enumeration ee
=v.elements();
        
while(ee.hasMoreElements())
        
{
            System.out.println(ee.nextElement()
+"");
            }

            System.out.println();
        }

    }


StackExample

package vector;

import java.util.*;

public class StackExample
{
    
public static void main(String args[])
    
{
        Stack s
=new Stack();
        System.out.println(
"大小:"+s.size());
        System.out.println(
"容量:"+s.capacity());
        s.push(
new Integer(2));
        s.push(
new Integer(4));
        s.push(
new Integer(6));
        s.push(
new Integer(8));
        System.out.println(
"当前容量:"+s.capacity());
        s.push(
new Double(10.04));
        s.push(
new Float(11.4));
        System.out.println(
"当前容量"+s.capacity());
        
if(s.contains(new Integer(2)))
        
{
            System.out.println(
"栈中包含2");
        }

        
while(!s.empty())
        
{
            System.out.println(s.pop());    
        }

    }

}



JDBC执行sqlserver存储过程,带输出参数

package dbObject;

import java.sql.*;

public class linkDB
{
    
public static void main(String args[])
    
{
        
try
        
{
        
            Class.forName(
"sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con
=java.sql.DriverManager.getConnection("jdbc:odbc:blogdb","sa","");
            java.sql.CallableStatement ca
=con.prepareCall("{call gettongji(?,?,?,?)}");
            ca.registerOutParameter(
1,java.sql.Types.INTEGER);
            ca.registerOutParameter(
2,java.sql.Types.INTEGER);
            ca.registerOutParameter(
3,java.sql.Types.INTEGER);
            ca.registerOutParameter(
4,java.sql.Types.INTEGER);
            ca.execute();
            System.out.println(ca.getInt(
1));
            System.out.println(ca.getInt(
2));
            System.out.println(ca.getInt(
3));
            System.out.println(ca.getInt(
4));
            
        
//    java.sql.Statement stet=con.createStatement();
        
//    String sql="insert into table1(address,age,sex) values('江西南昌',22,'男')";
        ///    stet.executeUpdate(sql);
        
//    PreparedStatement pstet=con.prepareStatement("insert into table1 values(?,?,?)");
        
//    pstet.addBatch();
        
//    pstet.setString(1,"北京");
        
//    pstet.setInt(2,80);
    
//        pstet.setString(3,"女");
    
//        pstet.executeUpdate();
        
//    stet.close();
        ca.close();
            con.close();
            
        }

        
catch(Exception ee)
        
{
            System.out.println(ee.getMessage());
        }

        
    }

    
}
 

##################################################

   今天就到这吧。。有空再整理。。
转载请注明:http://www.blogjava.net/wujun/


posted on 2006-05-11 00:44 record java and net 阅读(470) 评论(0)  编辑  收藏 所属分类: Jsp&&Web


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论