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 阅读(546) | 评论 (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 阅读(590) | 评论 (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 阅读(750) | 评论 (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 阅读(994) | 评论 (1)编辑 收藏

仅列出标题