zoukankan      html  css  js  c++  java
  • 菜鸟水平的jsp整理

    从去年9月份,我就开始着手学JSP,以前也只有一点程序的意识,一路上摸索过来,经过了很多磨难,终于有一天,我就像一个旱鸭子学会游泳一样,心里无比高兴,熬了几天夜,终于写成了这个纯JSP的文章发布程序。
    相信下面的几则小知识对向我这样水平的菜鸟有一定的帮助! 
        ==============================================================================
    1.传递表单参数:
    String name = new String(request.getParameter("name"));

    2.数据库连接:
    ~~MYSQL
    //设置数据库的URL
    String url = "jdbc:mysql://localhost:3306/jspsky";
    try
    //加载驱动程序
    Class.forname("org.gjt.mm.mysql.Driver").newInstance();
    //建立连接
    java.sql.Connection connection = java.sql.DriverManager.getConnection(url);
    java.sql.Statement statement = connection.createStatement();
    //SQL语句
    String sqlStringi ="insert into commu(name,tel,mobile,oicq,email)values(‘"+name+"’,‘"+tel+"’,‘"+mobile+"’,‘"+oicq+"’,‘"+email+"’)";
    //运行SQL语句,并建立结果集
    java.sql.ResultSet rsi = statement.executeQuery(sqlStringi);
    //在屏幕上输出库中的内容
    while(rss.next())
    {
    String a_name = rss.getString(1);
    out.println(a_name);
    {}
    //关闭连接
    connection.close();
    }

    //捕捉异常
    catch(java.sql.SQLException e)

    out.println(e.getMessage());
    {}
    catch(ClassNotFoundException e)

    out.println(e.getMessage());
    {}


    ~~DB2
    //定义数据库的URL
    String url = "jdbc:db2:portal";
    try

    //加载驱动程序
    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
    //建立连接,
    java.sql.Connection connection = java.sql.DriverManager.getConnection(url,"user","password");
    java.sql.Statement statement = connection.createStatement();
    //SQL语句
    String sqlString = "select * from client";
    //执行SQL语句
    java.sql.ResultSet rs = statement.executeQuery(sqlString);
    //在屏幕上显示所连表中的内容
    while(rs.next())
    {
    String name = rs.getString(1);
    out.println(name);
    {}
    //关闭连接
    connection.close();
    }
    //捕捉异常
    catch(java.sql.SQLException e)

    out.println(e.getMessage());
    {}
    catch(ClassNotFoundException e)

    out.println(e.getMessage());
    {}

    ms  SqlServer
    private String csDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    private String csJDBCUrl = "jdbc:microsoft:sqlserver://192.168.2.15:1433;User=XXX;Password=XXX;DatabaseName=XXX";
    另外把三个driver文件至少考到WEB-INF\lib\下面去

    orcale
    private String csDriver ="Oracle.oracle.jdbc.driver.OracleDriver";
    private String lsJDBCURl = "jdbc:oracle.thin:@" + psHostName +":" + psPort + ":" + psDatabase +"User=XXX;Password=XXX;";



    3.文件操作

    ~~将一个字符串写到一个指定的文件中,如果该文件不存在,则新建一个文件,并完成写入;如果存在,则用此字符串覆盖原文件的所有内容
    import java.io.*;
    String str = "print me 雪峰!";
    //定义好打印的目标文件名

    //取得当前主机存放WEB页面的绝对路径
    String hostdir = System.getProperty("user.dir");
    //取得当前主机所采用的路径分隔符
    String fileBar = System.getProperty("file.separator");
    //书写完整的目标文件存放路径
    String nameOfFile=hostdir+fileBar+"test.html";

    try
    //实例化一个文件输出流对象
    FileOutputStream afile = new FileOutputStream(nameOfFile);
    //将文件输出流,创建一个打印输出流对象
    PrintWriter pw = new PrintWriter(afile);
    pw.println(str);
    //clean up
    pw.close();
    {}
    catch(IOException e)
    out.println(e.getMessage());
    {}

    ~~列出指定目录下的文件列表
    import java.io.*;
    String cdur = System.getProperty("user.dir");
    String fileBar = System.getProperty("file.separator");
    String mydir =cdur+fileBar+"doc"+fileBar+"jspsky";
    File my = new File(mydir);
    String d[] = my.list();
    int i;
    int l=d.length;
    for(i=0;i out.print(d[i]);
    {}


    4.计数器
    Integer count = null;
    synchronized (application)
    count =(Integer) application.getAttribute("d");
    if (count ==null)
    count =new Integer("0");
    count = new Integer(count.intValue()+1);
    application.setAttribute("d",count);
    {}
    out.println(count);
    // 首先定义一个整形对象,并初始化为:NULL,
    // 取回APPLICATION对像的属性D的值,并强制转化为整形对象,赋给COUNT
    // 判断COUNT是否为空,为空时,将O赋给COUNT对象,
    // 否则,通过COUNT。INTVALUE()方法,实现COUNT对象加1,并赋值给COUNT
    // 最后,将COUNT对象保存在APPLICATION对象的D变量中。

    5、用于在tomcat,resin中解决中文乱码问题的函数
    <%!
    public String toChi(String input) {
    try {
    byte[] bytes = input.getBytes("ISO8859-1");
    return new String(bytes);
    }catch(Exception ex) {
    }
    return null;
    }

    public String toGb(String uniStr){
    String gbStr = "";
    if(uniStr == null){
    uniStr = "";
    }
    try{
    byte[] tempByte = uniStr.getBytes("ISO8859_1");
    gbStr = new String(tempByte,"GB2312");
    }
    catch(Exception ex){
    }
    return gbStr;
    }

    public String toUni(String gbStr){
    String uniStr = "";
    if(gbStr == null){
    gbStr = "";
    }
    try{
    byte[] tempByte = gbStr.getBytes("GB2312");
    uniStr = new String(tempByte,"ISO8859_1");
    }catch(Exception ex){
    }
    return uniStr;
    }

    %>

    用的时候:toGb(request.getParameter("RegionTown"))

  • 相关阅读:
    工作中问题的总结1
    linux问题故障
    时间转换
    Tips
    总结
    方向
    同步&异步-阻塞&非阻塞
    IO 之 mark()、reset()
    GC日志分析
    JDK 部分工具使用方法
  • 原文地址:https://www.cnblogs.com/liangqihui/p/212447.html
Copyright © 2011-2022 走看看