Access Oracle, SQLServer, DB2, Informix, Access Database in JSP

xiaoxiao2021-03-06  17

Accessing Oracle, SqlServer, DB2, Informix, Access Database in JSP, now there are many beginners JSP users often ask how to connect, how old is wrong? So I concentrated on this article for your reference, in fact, this kind of database logic is all

Not necessarily a good practice in JSP, but it is conducive to beginners to learn, so I do this. When you learn to a certain level, you can consider using MVC mode development. Exercise these

When you are code, you must put the JDBC driver in the server classpath, then build a table TEST in the database, there are two fields such as Test1, Test2, you can use the following

SQL Create Table Test (Test1 VARCHAR (20), Test2 Varchar (20), then write a test record to this table, then start our JSP and database trip

Bar.

First, JSP connection Oracle8 / 8i / 9i database (with Thin mode)

TestoCle.jsp is as follows: <% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.sql. *"%> <% class.forname (" Oracle.jdbc.driver.racledriver "). newinstance (); string url =" jdbc: oracle: th: @localhost: 1521: orc; // orcl for your database SID string user = "scott"; string password = "tiger"; Connection conn = DriverManager.getConnection (url, user, password); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select * from test"; ResultSet rs = stmt. ExecuteQuery (SQL); while (rs.next ()) {%> Your first field content is: <% = rs.getstring (1)%> Your second field content is: <% = RS. GetString (2)%> <%> <% out.print ("Database Operations Success, Congratulations");%> <% rs.close (); stmt.close (); conn.close ();% >

Second, JSP connection SQL Server7.0 / 2000 database

Testsqlserver.jsp is as follows: <% @ page contenttype = "text / html; charset = GB2312"%> <% @ page import = "java.sql. *"%> <% class.Forname (" com.microsoft.jdbc.sqlserver.sqlserverdriver "). NewInstance (); string url =" JDBC: Microsoft: SQLServer: // localhost: 1433; databasename = pubs "; // pubs for your database String User =" SA "; String password =" "; Connection conn = DriverManager.getConnection (url, user, password); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql =" select * from test "; ResultSet rs = Stmt.executeQuery (SQL); while (rs.next ()) {%> Your first field content is: <% = rs.getstring (1)%> Your second field content is: <% = rs.getstring (2)%> <%}%> <% out.print ("Database operation success, congratulations");%> <% rs.close (); stmt.close (); conn.close ( );%> three, JSP connection DB2 database

Testdb2.jsp is as follows: <% @ page contenttype = "text / html; charset = GB2312"%> <% @ page import = "java.sql. *"%> <% class.Forname (" com.ibm.db2.jdbc.app.db2driver ") .newinstance (); string URL =" JDBC: DB2: // localhost: 5000 / sample "; // Sample for your database name string user =" admin "; String password = ""; Connection conn = DriverManager.getConnection (url, user, password); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select * from test"; ResultSet rs = stmt .executeQuery (SQL); while (rs.next ()) {%> Your first field content is: <% = rs.getstring (1)%> Your second field content is: <% = RS .getstring (2)%> <%}%> <% out.print ("Database operation success, congratulations");%> <% rs.close (); stmt.close (); conn.close (); %> four, JSP connection Informix database

Testinformix.jsp is as follows: <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ page import = "java.sql. *"%> <% class.forname (" com.informix.jdbc.IfxDriver ".) newInstance (); String url =" jdbc: informix-sqli: //123.45.67.89: 1533 / testDB: INFORMIXSERVER = myserver; user = testuser; password = testpassword "; // testDB is the name of your database Connection conn = DriverManager.getConnection (url); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select * from test"; ResultSet rs = stmt.executeQuery (sql) WHILE (rs.next ()) {%> Your first field content is: <% = rs.getstring (1)%> Your second field content is: <% = rs.getstring (2) %> <%}%> <% Out.print ("Database Operation, Congratulations");%> <% rs.close (); stmt.close (); conn.close ();%> 5, JSP connection access database

转载请注明原文地址:https://www.9cbs.com/read-45110.html

New Post(0)