導航:首頁 > 編程大全 > servlet資料庫連接

servlet資料庫連接

發布時間:2025-05-30 13:28:44

Ⅰ 怎樣用servlet進行對資料庫的操作

1、首先明確一點,servlet就是一個繼承自javax.servlet.http.HttpServlet的類。

2、範例:使用servlet連接資料庫

packagecom.oracle.project.servlet;

importjava.util.*;
importjava.io.*;
importjava.sql.*;

importjavax.servlet.*;
importjavax.servlet.http.*;
importjavax.servlet.annotation.*;

@WebServlet("/TestServlet")

{
="oracle.jdbc.driver.OracleDriver";
publicstaticfinalStringDBURL="jdbc:oracle:thin:@localhost:1521:ORACLEDB";
publicstaticfinalStringDBUSER="scott";
="tiger";
privateConnectionconn=null;

publicConnectiongetConnection(){
try{
if(this.conn==null){
Class.forName(DBDRIVER);
this.conn=DriverManager.getConnection(DBURL,DBUSER,PASSWORLD);
}
}catch(Exceptione){
e.printStackTrace();
}
returnconn;
}

@Override
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
PrintWriterout=response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>"+this.getConnection()+"</h1>");
out.println("</body>");
out.println("</html>");
}
@Override
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
this.doGet(request,response);
}
}
閱讀全文

與servlet資料庫連接相關的資料

熱點內容
qq頭像的女生眼淚 瀏覽:595
核桃編程如何設置積分 瀏覽:868
linux文件遷移 瀏覽:284
sql分組最後一條資料庫 瀏覽:296
javaepollsocket 瀏覽:713
qq群文件夾看不到 瀏覽:77
yy攝像頭怎麼調教程 瀏覽:146
yy文件在哪裡 瀏覽:740
qq氣泡框上字體代碼 瀏覽:986
資料庫基礎考題 瀏覽:727
網站排名怎麼報價 瀏覽:231
matlab代跑程序 瀏覽:738
百寶app是做什麼的 瀏覽:325
vb程序比較大小程序 瀏覽:951
clinuxsleep頭文件 瀏覽:342
蘋果6swifi模塊 瀏覽:744
3d的備份文件在哪裡 瀏覽:824
硬體編程的硬體哪個好 瀏覽:534
數控編程按車床分類有哪些 瀏覽:275
qq文件下載到哪裡 瀏覽:434

友情鏈接