cavenaghi

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            4 Posts :: 0 Stories :: 3 Comments :: 0 Trackbacks

          2005年7月27日 #

          下面的是幾種常見數據庫系統的數據庫連接方式

          系統環境:win2003 + j2se5.0 + tomcat5.5.7

          前提是你已經從網上已經下載了各個數據庫的JDBC支持!
          把那些主要的工具包都放到Tomcat 5.5\common\lib就行!
          刨除那些入門的東西吧,現在開始步入正題!
          為了減少篇幅,更為了把問題說的直觀,我已經把異常捕捉代碼給去除了!

          1.Microsoft SQL Server 2000

           Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
           Connection conn= DriverManager.getConnection
            ("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=數據庫名字","sa","pwd");
           Statement stmt=conn.createStatement();
           //增加和查詢語句
           stmt.executeUpdate("insert into boya values('mssql','2000')");
           ResultSet rs=statement.executeQuery("select * from boya");
           //顯示記錄
           while(rs.next()){
            out.print(rs.getString(1)+"  "+rs.getString(2));
            out.println("<br>");
           }

          2.MySQL 5.0

           Class.forName("com.mysql.jdbc.Driver");
           Connection conn = DriverManager.getConnection
            ("jdbc:mysql://localhost/數據庫名字?user=root&password=pwd");
           Statement stmt=conn.createStatement();
           //增加和查詢語句
           stmt.executeUpdate("insert into boya values('mysql','5.0')");
           ResultSet rs=stmt.executeQuery("select * from boya");
           //顯示記錄
           while(rs.next()){
            out.print(rs.getString(1)+"  "+rs.getString(2));
            out.println("<br>");
           }

          3.PostgreSQL 8.0

           //這個服務器的默認端口5432,如果更改了,代碼也需要更改
           Class.forName("org.postgresql.Driver");
           Connection conn=DriverManager.getConnection
            ("jdbc:postgresql://localhost:5432/數據庫名字?user=admin&&password=pwd");
           Statement stmt=conn.createStatement();
           //增加和查詢語句
           stmt.executeUpdate("insert into boya values('mysql','5.0')");
           ResultSet rs=stmt.executeQuery("select * from boya");
           //顯示記錄
           while(rs.next()){
            out.print(rs.getString(1)+"  "+rs.getString(2));
            out.println("<br>");
           }

          4.ODBC

           //in my opinion 會寫ODBC就夠了,現在PostgreSQL、MYSQL里也都有ODBC支持,但速度慢啊!
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           Connection conn=DriverManager.getConnection("jdbc:odbc:test","","");
           Statement stmt=conn.createStatement();
           //增加和查詢語句
           stmt.executeUpdate("insert into boya values('mysql','5.0')");
           ResultSet rs=stmt.executeQuery("select * from boya");
           //顯示記錄
           while(rs.next()){
            out.print(rs.getString(1)+"  "+rs.getString(2));
            out.println("<br>");
           }

          posted @ 2005-07-27 15:19 Cavenaghi 閱讀(569) | 評論 (0)編輯 收藏

          在JSP網頁上實現月歷

          JSP、月歷

          <%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,java.text.*" %>
          <html>
          <head>
          <title>月歷</title>
          </head>
          <body style="font-size:12px">
          <%!
           /*聲明變量*/
           String[] months = {"January", "February", "March", "April", "May", "June",
                  "July", "August", "September", "October", "November", "December"};
           String[] months_cn = {"一月", "二月", "三月", "四月", "五月", "六月",
                     "七月", "八月", "九月", "十月", "十一月", "十二月"};
           public final static int dom[] = {
                   31, 28, 31, 30,
                   31, 30, 31, 31,
                   30, 31, 30, 31
           };
          %>
          <%
           /*處理事件*/
           boolean yyok = false;
           int yy = 0, mm = 0;
           String yyString = request.getParameter("year");
           if (yyString != null && yyString.length() >0) {
            try {
             yy=Integer.parseInt(yyString);
             yyok=true;
            }
            catch (NumberFormatException e) {
             out.println("年份不可用");
            }
            Calendar cal = Calendar.getInstance();
            if (!yyok)
             yy = cal.get(Calendar.YEAR);
            String mmString = request.getParameter("month");
            if (mmString == null) {
             mm = cal.get(Calendar.MONTH);
            }
            else {
             for (int i = 0; i < months.length; i++)
              if (months[i].equals(mmString)) {
               mm = i;
               break;
              }
            }
           }
          %>
          <form name="cal" method=post action="cal.jsp">
           請選擇月份:
            <select name="month">
             <%
              /*初始化表單*/
             for (int i = 0; i < months.length; i++) {
              if (i == mm)
               out.print("<option selected value=January>");
              else
               out.print("<option value="+months[i]+">");
              out.print(months_cn[i]);
              out.print("</option>");
             }
             %>
            </select>
           <br>請輸入年份:
             <input type="text" size="5" name="year" value="<%=yy%>">&nbsp;&nbsp;
             <input type="submit" value="顯示">
          </form>
          <%
           int lead = 0;
          %>
          <table border="0" cellpadding="1" cellspacing="1" style="font-size:12px">
          <tr height="20"><td colspan="7"><font color="#3399FF"><b><%= months[mm]%>  <%= yy%></b></font></td></tr>
          <% GregorianCalendar calendar =  new GregorianCalendar(yy, mm ,1); %>
          <tr><td><font color="#FF0000">Sun</font></td><td>Mon</td><td>Tue</td><td>Wed</td>
          <td>Thu</td><td>Fri</td><td><font color="#FF0000">Sat</font></td></tr>
          <%
           /*下面是顯示月歷的代碼*/
           lead = calendar.get(Calendar.DAY_OF_WEEK)-1;
           int dayInMonth = dom[mm];
           if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && mm == 1)
            ++dayInMonth;
           out.print("<tr>");
           for(int i = 0; i < lead; i++) {
             out.print("<td>&nbsp;</td>");
           }
           for(int i = 1; i <= dayInMonth; i++) {
            if ((i+lead) % 7 == 0 || (i+lead) % 7 == 1)
             out.print("<td align=\"center\"><font color=\"#FF0000\">"+i+"</font></td>");
            else
             out.print("<td align=\"center\">"+i+"</td>");
            if ((lead+i) % 7 == 0) {
             out.print("</tr></tr>");
            }
           }
           out.print("</tr>");
          %>
          </table>
          </body>
          </html>

          posted @ 2005-07-27 11:58 Cavenaghi 閱讀(614) | 評論 (1)編輯 收藏

          迷宮文件boya.ice:

          8
          9
          #########
          #s0##0###
          #0##00###
          #0##0####
          #0000####
          #0##0####
          #0##00e##
          #0000####

           

          package maze;
          import java.io.*;
          import java.util.*;
          public class Maze{
           private char[][] maze;//迷宮數組
           private int startX,startY,endX,endY;//迷宮起點,終點的位置
           private int x,y,step=0;//迷宮長寬及步驟
           //依據輸入的文件名創建對象
           private Maze(String fileName){
            try{
             LinkedList aList=new LinkedList();//用于存儲文件每行的內容
             BufferedReader files=new BufferedReader(new FileReader("map\\"+fileName));
             //將每行的內容依次加入到LinkedList中
             String temp=new String();
             int i=0;
             while((temp=files.readLine())!=null){
              aList.add(temp);
             }
             files.close();
             //讀取并設置迷宮的長寬
             x=Integer.parseInt((String)aList.getFirst())+2;
             aList.removeFirst();
             y=Integer.parseInt((String)aList.getFirst())+2;
             aList.removeFirst();
             //依據長寬對迷宮進行初始化
             maze=new char[x][y];
             //將迷宮的賦予外圍墻
             for(i=0;i<x;i++){
              maze[i][0]='#';
              maze[i][y-1]='#';
             }
             for(i=0;i<y;i++){
              maze[0][i]='#';
              maze[x-1][i]='#';
             }
             //將LinkedList中內容讀入數組
             Iterator it=aList.iterator();
             i=1;
             char[] row;
             while(it.hasNext()){
              temp=((String)it.next());
              row=new char[y-2];
              row=temp.toCharArray();
              for(int j=1;j<y-1;j++){
               maze[i][j]=row[j-1];
               if(maze[i][j]=='s'){
                startX=i;
                startY=j;
                maze[i][j]='0';
               }
               else if(maze[i][j]=='e'){
                endX=i;
                endY=j;
                maze[i][j]='0';
               }
              }
              i++;
             }
            }
            catch(FileNotFoundException e){
             System.out.println("File Name Input Wrong!!!");
            }
            catch(IOException e){
             System.out.println("Wrong Input!!!");
            }
           }
           //遞歸方法尋找路徑
           private boolean findWay(int x,int y){
            if(maze[endX][endY]=='i')
             return true;
            else
             if(maze[x][y]=='0'){
              maze[x][y]='i';
              if(findWay(x-1,y))
               return true;
              else if(findWay(x+1,y))
               return true;
              else if(findWay(x,y+1))
               return true;
              else if(findWay(x,y-1))
               return true;
              else{
               maze[x][y]='c';
               return false;
              }
             }
             else return false;
           }
           //打印迷宮路徑
           private void show(){
            maze[startX][startY]='s';
            maze[endX][endY]='e';
            for(int i=1;i<x-1;i++){
             for(int j=1;j<y-1;j++){
              if(maze[i][j]=='i'){
               maze[i][j]=' ';
               step++;
              }
              else if(maze[i][j]=='c') maze[i][j]='0';
              System.out.print(maze[i][j]);
             }
             System.out.println("");
            }
            System.out.println("I Have went "+step+" Steps To The End!");
           }
           public static void main(String arg[]){
            try{
             System.out.println("Boya(8*9)\n"+"Ice(10*12)\n"+"Sky(15*17)\n"+"Input the map name:");
             BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
             for(;;){
              String input=new String();
              input=is.readLine().trim();
              if(input.equals("q")) break;
              else{
               Maze boya=new Maze(input+".ice");
               if(boya.findWay(boya.startX,boya.startY)){
                boya.show();
               }
               else System.out.println("No Ways to the end!");
              }
              System.out.println("Input another map name or input 'q' to quit:");
             }
             is.close();
            }
            catch(IOException e){
             System.out.println("Wrong Input!!!");
            }
            catch(NullPointerException e){
             System.out.println("Wrong Input!!!");
            }
           }
          }

          posted @ 2005-07-27 11:54 Cavenaghi 閱讀(770) | 評論 (1)編輯 收藏

          package expression;
          public class Calculate{
           public static boolean isOperator(String operator){
            if(operator.equals("+")||operator.equals("-")||operator.equals("*")||operator.equals("/")||operator.equals("(")||operator.equals(")")) return true;
            else return false;
           }
           public static int priority(String operator){
            if(operator.equals("+")||operator.equals("-")||operator.equals("(")) return 1;
            else if(operator.equals("*")||operator.equals("/")) return 2;
            else return 0;
           }
           public static String twoResult(String operator,String a,String b){
            try{
             String op=operator;
             String rs=new String();
             double x=Double.parseDouble(b);
             double y=Double.parseDouble(a);
             double z=0;
             if(op.equals("+")) z=x+y;
             else if(op.equals("-")) z=x-y;
             else if(op.equals("*")) z=x*y;
             else if(op.equals("/")) z=x/y;
             else z=0;
             return rs+z;
            }
            catch(NumberFormatException e){
             System.out.println("input has something wrong!");
             return "Error";
            }
           }
          }

           

           

          package expression;
          import java.util.*;
          public class Stacks{
           private LinkedList list=new LinkedList();
           int top=-1;
           public void push(Object value){
            top++;
            list.addFirst(value);
           }
           public Object pop(){
            Object temp=list.getFirst();
            top--;
            list.removeFirst();
            return temp;

           }
           public Object top(){
            return list.getFirst();
           }
          }

           

          package expression;
          import java.io.*;
          import java.util.*;
          public class Expression{
           private ArrayList expression=new ArrayList();//存儲中序表達式
           private ArrayList right=new ArrayList();//存儲右序表達式
           private String result;//結果
           //依據輸入信息創建對象,將數值與操作符放入ArrayList中
           private Expression(String input){
            StringTokenizer st=new StringTokenizer(input,"+-*/()",true);
            while(st.hasMoreElements()){
             expression.add(st.nextToken());
            }
           }
           //將中序表達式轉換為右序表達式
           private void toRight(){
            Stacks aStack=new Stacks();
            String operator;
            int position=0;
            while(true){
             if(Calculate.isOperator((String)expression.get(position))){
              if(aStack.top==-1||((String)expression.get(position)).equals("(")){
               aStack.push(expression.get(position));
              }
              else{
               if(((String)expression.get(position)).equals(")")){
                if(!((String)aStack.top()).equals("(")){
                 operator=(String)aStack.pop();
                 right.add(operator);
                }
               }
               else{
                if(Calculate.priority((String)expression.get(position))<=Calculate.priority((String)aStack.top())&&aStack.top!=-1){
                 operator=(String)aStack.pop();
                 if(!operator.equals("(")) right.add(operator);
                }
                aStack.push(expression.get(position));
               }
              }
             }
             else right.add(expression.get(position));
             position++;
             if(position>=expression.size()) break;
            }
            while(aStack.top!=-1){
             operator=(String)aStack.pop();
             right.add(operator);
            }
           }
           //對右序表達式進行求值
           private void getResult(){
            this.toRight();
            Stacks aStack=new Stacks();
            String op1,op2,is=null;
            Iterator it=right.iterator();
            while(it.hasNext()){
             is=(String)it.next();
             if(Calculate.isOperator(is)){
              op1=(String)aStack.pop();
              op2=(String)aStack.pop();
              aStack.push(Calculate.twoResult(is,op1,op2));
             }
             else aStack.push(is);
            }
            result=(String)aStack.pop();
            it=expression.iterator();
            while(it.hasNext()){
             System.out.print((String)it.next());
            }
            System.out.println("="+result);
           }
           public static void main(String avg[]){
            try{
             System.out.println("Input a expression:");
             BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
             for(;;){
              String input=new String();
              input=is.readLine().trim();
              if(input.equals("q")) break;
              else{
               Expression boya=new Expression(input);
               boya.getResult();
              }
              System.out.println("Input another expression or input 'q' to quit:");
             }
             is.close();
            }
            catch(IOException e){
             System.out.println("Wrong input!!!");
            }
           }
          }

          posted @ 2005-07-27 11:45 Cavenaghi 閱讀(1012) | 評論 (1)編輯 收藏

          僅列出標題  
          主站蜘蛛池模板: 新津县| 临沂市| 临清市| 浪卡子县| 江口县| 大宁县| 浦城县| 福州市| 汉寿县| 太仓市| 留坝县| 灵川县| 博野县| 晋中市| 庆阳市| 枞阳县| 阳东县| 峨眉山市| 满洲里市| 岳池县| 长治市| 朝阳区| 庆阳市| 莱西市| 宁乡县| 闵行区| 依兰县| 藁城市| 潜江市| 清流县| 长白| 嘉定区| 呈贡县| 当阳市| 江安县| 乌兰浩特市| 天水市| 深水埗区| 宣城市| 禹城市| 汝阳县|