草木瓜 2006-5-26
一、前言
jsp開發,一般來說需要前端開發工具和后臺服務。weblogic是集成了工具和服務的大型系統。
需要重視的是,bea公司的weblogic最低要求配置內存512m,即使對服務器參數文件做優化,
仍然不能根本解決問題。
二、安裝配置weblogic
windows安裝沒什么好說的。
安裝后,weblogic自動建了三個服務器(workshop,integration,portal),用戶可以選擇,不爽的
也可以自行創建,創建方面的內容《weblogic domain 配置方法》一文已有非常詳盡的講述。簡單的
方法是從模板選所擇,這里選擇 工具->weblogic server->配置向導 的basic weblogic workshop
domain模板。
三、新建應用程序,添加web項目,
域服務器創建好了,選擇 文件->新建->應用程序,選擇服務囂,選擇應用程序,完成創建。在所建
應用程序里添加web項目。
四、web應用
添加數據庫web應用。在web項目里,添加jsp文件和java class。本例使用index.jsp,error.jsp,
clsdb.java,db.properties四個文件。
目錄結構如下:
應用程序名/web項目名/web-inf/...
應用程序名/web項目名/index.jsp
應用程序名/web項目名/error.jsp
應用程序名/web項目名/db.properties
應用程序名/web項目名/javacls/clsdb.java
index.jsp 起始頁面,數據瀏覽
--------------------------------------------------
<body>
<%
javacls.clsdb db=new javacls.clsdb();
boolean i;
i=db.openconnection();
if(i=true)
{
java.sql.resultset rs=db.exequery("select d from test");
rs.next();
while(!rs.isafterlast())
{
out.println(rs.getobject(1));
rs.next();
}
}
%>
</body>
error.jsp 錯誤顯示頁面
--------------------------------------------------
<p>
發生錯誤
<br>
錯誤描述:
<%=exception.tostring()%>
<br>
錯誤原因:
<%=exception.getmessage()%>
</p>
clsdb.java 數據庫操作類,非常典型的數據庫應用方法
--------------------------------------------------
package javabean;
public class clsdb
{
java.sql.connection cn=null;
java.sql.statement sqlstm=null;
java.sql.resultset rs=null;
public clsdb()
{}
//打開數據庫連接
public boolean openconnection()
{
//讀取設置
java.util.properties prop=new java.util.properties();
try
{
java.io.inputstream in=this.getclass().getresourceasstream("../db.properties");
prop.load(in);
if(in!=null)in.close();
}
catch(java.io.ioexception e)
{
system.out.println("[opencn] 配置文件打開錯誤! ");
return false;
}
string jdbc=prop.getproperty("drivers");
string url=prop.getproperty("url");
string user=prop.getproperty("user");
string password=prop.getproperty("password");
//加載jdbc
try
{
class.forname(jdbc);
}
catch(java.lang.classnotfoundexception e)
{
system.out.println("[opencn] 裝載jdbc驅動出錯! ");
return false;
}
//打開數據庫連接
try
{
this.cn=java.sql.drivermanager.getconnection(url,user,password);
}
catch(exception e)
{
e.printstacktrace();
return false;
}
return true;
}
//執行查詢
public java.sql.resultset exequery(string _sqlstring)
{
try
{
this.sqlstm=this.cn.createstatement();
this.rs=this.sqlstm.executequery(_sqlstring);
return this.rs;
}
catch(exception e)
{
e.printstacktrace();//此外用于打印錯誤堆棧
return null;
}
}
//執行更新
public void exenonquery(string _sqlstring) throws java.sql.sqlexception
{
this.sqlstm=this.cn.createstatement();
this.sqlstm.executeupdate(_sqlstring);
if(this.sqlstm!=null)this.sqlstm.close();
}
//關閉對象
public void close() throws java.sql.sqlexception
{
if(this.rs!=null)this.rs.close();
if(this.sqlstm!=null)this.sqlstm.close();
if(this.cn!=null)this.cn.close();
}
protected void finalize() throws throwable
{
this.close();
}
}
db.properties 數據庫配置文件
--------------------------------------------------
drivers=oracle.jdbc.driver.oracledriver
url=jdbc:oracle:thin:@localhost:1521:wincn
user=liwei
password=liwei
五、重要補充說明:本篇文章別的都是廢話,惟獨這段不是!
本例使用的jdbc驅動是oracle提供的,java只提供驅動的接口,具體必須由各數據庫廠商來實現。如果
環境變量等沒有配置正確,找不到包,可以在 應用程序名/web項目名/庫 下添加驅動包。
drivers=oracle.jdbc.driver.oracledriver這個包的位置在e:/oracle/ora92/jdbc/lib/classes12.jar
查看包的信息就知道,寫成oracle.jdbc.oracledriver也是可以的。
url一項內容是數據庫的具體對象,前面jdbc:oracle:thin表明是通過jdbc,而且是oracle提供的驅動包,
thin是oracle的連接方式。下面@后面是主機名或主機地址+端口,最后一項是數據庫實例名sid。
注意:
在調試過程中weblogic會提示找不到類oracle.jdbc.dirver.oracledriver的文件,這個提示完全是誤報,
是不是bug不得而知!不用理會繼續執行!
在連接中文字符集(zhs16gbk)數據庫不會出現亂碼,如果連接英文字符集(us7ascii)則會出現。解決方法
是作些必要的轉換。前提是客戶端與服務囂的字符集要一致。
index.jsp 文件內容更改
while(!rs.isafterlast())
{
string test=rs.getstring(1);
byte[] tempbyte=test.getbytes("iso8859-1");
string temp=new string(tempbyte,"gb2312");
out.println(temp);
rs.next();
}
新聞熱點
疑難解答