Java實(shí)現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法
本文實(shí)例講述了Java實(shí)現(xiàn)JSP使用Servelt連接Oracle數(shù)據(jù)庫的方法。為了實(shí)現(xiàn)在Servlet 中連接數(shù)據(jù)庫,必須編寫Servlet 的類程序。將Servlet 類程序放到WEB 服務(wù)器的servlets 或者classes 目錄下面,為調(diào)用Servlet,需要?jiǎng)?chuàng)建發(fā)送Servlet 請求的HTML 文檔。本例通過(Driver)Class.forName(driverName).newInstance()方法來實(shí)現(xiàn)加載驅(qū)動(dòng)程序,建立與數(shù)據(jù)庫連接。
具體程序代碼為:
1.Database 類繼承了HttpServlet 類,共有兩個(gè)方法:doGet()和displayResult(),代碼如下:
public class Database extends HttpServlet
{
public void doGet();
public void displayResult(ResultSet results,PrintWriter out);
}
2.在doGet()方法中建立與數(shù)據(jù)庫的連接并執(zhí)行查詢:
public void doGet()
{
HttpServletRequest request,
HttpServletResponse response
}
throws ServletException, IOException
{
PrintWriter out;
String title = "Simple Servlet connecting to Oracle DB";
response.setContentType("text/html;charset=GB2312");
out = response.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY>");
out.println("<H1>" + title + "</H1>");
out.println("<P>This is output from SimpleServlet.");
String driverName = "oracle.jdbc.driver.OracleDriver";
Driver d;
Connection con;
Statement stmt;
ResultSet results;
try
{
d = (Driver)Class.forName(driverName).newInstance();
con = DiverManager.getConnection("jdbc:oracle:thin:ndb/ndb@192.168.1.6:1521:PC6");
stmt = con.createStatement();
String sqlstr = "select * from data";
results = stmt.executeQuery(sqlstr);
displayResult(results,out);
stmt.close();
con.close();
}
catch (Exception e)
{
out.println("error: " + e.toString());
}
out.println("</BODY></HTML>");
out.close();
}
3.DisplayResult()方法顯示查詢結(jié)果:
public void displayResult(ResultSet results,PrintWriter out)
{
StringBuffer buf = new StringBuffer();
String temp;
try
{
ResultSetMetaData rsmd = results.getMetaData();
int numCols = rsmd.getColumnCount();
int i, rowcount = 0;
for (i=1; i <= numCols; i++)
{
if (i > 1) buf.append(",");
buf.append(rsmd.getColumnLabel(i));
}
buf.append("");
while (results.next() && rowcount < 100)
{
for (i=1; i <= numCols; i++)
{
if (i > 1) buf.append(",");
buf.append((results.getString(i)));
}
buf.append("<br>");
rowcount++;
}
out.println("<br>");
out.println(buf.toString());
results.close();
}
catch (Exception e)
{
out.println("error: " + e.toString());
return;
}
}
4.因?yàn)槌绦蚴褂昧薐DBC 類、servlet 類和使用控制臺輸出,所以需要引入如下的包:
import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
5.編譯Database.java,產(chǎn)生Database.class 文件,將Database.class 放到WEB 服務(wù)器的servlets 目錄下,本例采用Java Web Server 作為WEB 服務(wù)器。配置好WEB 服務(wù)器,添加database.class,指定名稱為database。
6.編寫調(diào)用Servlet 的database.html 文件。代碼如下:
<html> <head> <title>Jsp使用Servlet連接數(shù)據(jù)庫</title> </head> <body> <center> <form action="/servlet/database" method="get"> <input name="action" type="submit" value="連接數(shù)據(jù)庫"> </form> </center> </body> </html>
相關(guān)文章
SpringBoot?+?MyBatis-Plus構(gòu)建樹形結(jié)構(gòu)的幾種方式
在實(shí)際開發(fā)中,很多數(shù)據(jù)都是樹形結(jié)構(gòu),本文主要介紹了SpringBoot?+?MyBatis-Plus構(gòu)建樹形結(jié)構(gòu)的幾種方式,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
SpringBoot的配置文件(properties與yml)使用方法
配置文件中的配置類型有兩類,一類是系統(tǒng)配置項(xiàng),這種配置的格式都是固定的,是給系統(tǒng)使用的,另一種是用戶自定義配置,用戶可以隨意地規(guī)定配置項(xiàng)的格式,又用戶自行去設(shè)置和讀取,這篇文章主要介紹了SpringBoot的配置文件(properties與yml)使用方法,需要的朋友可以參考下2023-08-08
Java連接FTP服務(wù)器并使用ftp連接池進(jìn)行文件操作指南
使用FTP最主要的功能是對文件進(jìn)行管理,下面這篇文章主要給大家介紹了關(guān)于Java連接FTP服務(wù)器并使用ftp連接池進(jìn)行文件操作的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
使用@Valid+BindingResult進(jìn)行controller參數(shù)校驗(yàn)方式
這篇文章主要介紹了使用@Valid+BindingResult進(jìn)行controller參數(shù)校驗(yàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot?使用AOP?+?Redis?防止表單重復(fù)提交的方法
Spring?Boot是一個(gè)用于構(gòu)建Web應(yīng)用程序的框架,通過AOP可以實(shí)現(xiàn)防止表單重復(fù)提交,本文介紹了在Spring?Boot應(yīng)用程序中使用AOP和Redis來防止表單重復(fù)提交的方法,需要的朋友可以參考下2023-04-04

