Get the App
SLTechnology News&Howtos  ›  Development  › 

How Servlet connects to Oracle

Shulou Source: shulou.com Published: 2022-06-02 13:34:33 10月26日 Update

This article is about how Servlet connects to Oracle. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Copy the driver $ORACL_HOME/jdbc/lib/ojdbc6.jar of oracle to D:\ Tomcat5.5.17\ webapps\ my\ WEB-INF\ lib. (because the jar package is required at run time)

Package test

Import java.io.IOException

Import java.io.PrintWriter

Import javax.servlet.ServletException

Import javax.servlet.http.HttpServlet

Import javax.servlet.http.HttpServletRequest

Import javax.servlet.http.HttpServletResponse

Import java.sql.*

Public class Conndb extends HttpServlet {

Public void doGet (HttpServletRequest request, HttpServletResponse response)

Throws ServletException, IOException {

Connection conn = null

Statement stmt = null

ResultSet rs = null

Response.setContentType ("text/html")

Response.setCharacterEncoding ("gb2312")

PrintWriter out = response.getWriter ()

Out.println ("")

Out.println ("Content:")

Try {

Class.forName ("oracle.jdbc.driver.OracleDriver")

Conn = DriverManager.getConnection ("jdbc:oracle:thin:@192.168.1.250:1521:test", "scott", "tiger")

Stmt = conn.createStatement ()

Rs = stmt.executeQuery ("select * from emp")

While (rs.next ()) {

Out.println ("")

Out.println ("" + rs.getString ("ename") + "")

Out.println ("" + rs.getString ("MGR") + "")

Out.println ("")

}

Out.println ("")

} catch (ClassNotFoundException e) {

E.printStackTrace ()

} catch (SQLException e) {

E.printStackTrace ()

} finally {

Try {

If (rs! = null) {

Rs.close ()

Rs = null

}

If (stmt! = null) {

Stmt.close ()

Stmt= null

}

If (conn! = null) {

Conn.close ()

Conn = null

}

} catch (SQLException e) {

E.printStackTrace ()

}

}

}

}

Thank you for reading! This is the end of the article on "how to connect Servlet to Oracle". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Content more article good practical copy article see knowledge reference help related run drive Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi MariaDB vpn Shulou Information