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

xiaoxiao2021-03-06  34

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

Now there are a lot of first learning JSP users often ask how to connect the database, how old is wrong? So I concentrated on this article for your reference. In fact, this kind of database logic is not necessarily good in JSP, but it is conducive to beginners to learn, so I will do this, when you learn a certain degree At the time, you can consider the development of MVC. When you practice these 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 build Create Table Test with the following SQL (Test1 Varchar (20), Test2 Varchar (20), then writes a test record to this table, then start our JSP and database trip.

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-58799.html

New Post(0)