blogjava's web log

          blogjava's web log
          ...

          初學(xué)者有用.整理一些初級代碼


          連結(jié)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
          =%t?%p?-?%m%n?%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(
          "向量元素?cái)?shù)為:"+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();
          ????????
          ????}

          ????????
          }

          #######################################################################
          ???????????????????????? 轉(zhuǎn)載請注名出處:http://www.aygfsteel.com/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(
          "當(dāng)前容量:"+v.capacity());
          ????????v.addElement(
          new?Double(10.04));
          ????????v.addElement(
          new?Float(11.4));
          ????????System.out.println(
          "當(dāng)前容量:"+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(
          "當(dāng)前容量:"+s.capacity());
          ????????s.push(
          new?Double(10.04));
          ????????s.push(
          new?Float(11.4));
          ????????System.out.println(
          "當(dāng)前容量"+s.capacity());
          ????????
          if(s.contains(new?Integer(2)))
          ????????
          {
          ????????????System.out.println(
          "棧中包含2");
          ????????}

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

          ????}

          }



          JDBC執(zhí)行sqlserver存儲過程,帶輸出參數(shù)

          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());
          ????????}

          ????????
          ????}

          ????
          }
          ?

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

          ???今天就到這吧。。有空再整理。。
          轉(zhuǎn)載請注明:http://www.aygfsteel.com/wujun/


          posted on 2006-05-11 00:44 record java and net 閱讀(474) 評論(0)  編輯  收藏 所屬分類: Jsp&&Web

          導(dǎo)航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態(tài)語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 望谟县| 固始县| 安宁市| 左贡县| 宜兴市| 陆丰市| 万全县| 阿巴嘎旗| 惠来县| 宁都县| 东平县| 阿合奇县| 夏河县| 梁山县| 绥棱县| 察哈| 德化县| 唐山市| 米易县| 侯马市| 建阳市| 天柱县| 衡水市| 崇义县| 麻城市| 改则县| 治多县| 广西| 明星| 左权县| 南京市| 嘉善县| 尚志市| 平遥县| 府谷县| 红安县| 高平市| 布拖县| 福建省| 玉田县| 平原县|