jsp database connection Encyclopedia Issue Date: 2004-08-11 OF: java831 (desired) a, jsp connected Oracle8 / 8i / 9i database (with thin mode) testoracle.jsp follows: <% @ page contentType = "text / html; charset = GB2312 "%> <% @ page import =" java.sql. * "%>
<% class.forname (" Oracle.jdbc.driver.Oracledriver "). NewInstance (); string URL = "JDBC: Oracle: Thin: @localhost: 1521: ORCL"; // ORCL for your database Sidstring 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 The 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 ();%> body> html> 2, JSP connection SQL Server7.0 / 2000 Database / RTESTSQLSERVER.JSP as follows: <% @ page contenttype = "text / html; charset = GB2312"%> <% @ Page Import = "java.sql. *"%> <% Class .forname ("com.microsoft.jdbc.sqlser.sqlserverdriver). NewInstance (); string url =" JDBC: Microsof T: 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 is successful, congratulations you /");%> <% rs.close (); stmt.close (); conn.close ();%> body> html> 3, JSP connection DB2 database /RTESTDB2.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 / rstring 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 is successful, congratulations for you / "); %> %RS.close();stmt.close();stmt.close();stmt.close();stmt.close ();%> body> html> 4, JSP connection Informix database /rtestinformix.jsp as follows: <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ page import = "java . SQL. * "%> <% class.forname (" com.informix.j Dbc.ifxdriver "). NewInstance (); string url =" jdbc: informix-sqli: //123.45.67.89: 1533 / testdb: informixserver = myserver; user = testuser; password = testpassword "; // testdb for your database Name / RCONNECTION CONN = DriverManager.getConnection (URL); statement stmt = conn.createStatement (ResultSet.Type_Scroll_Sensitive, ResultSet.concur_updata); string sql = "Select * from test "; ResultSet RS = stmt.executeQuery (SQL); while (rs.next ()) {%> Your first field content is: <% = rs.getstring (1)%> Your The two fields are: <% = rs.getstring (2)%> <%}%> <% out.print ("Database operation is successful, congratulations for you / ");% "%RS.Close();stmt .Close (); conn.close ();%> body> html> 5, JSP connection sybase database /rtestmysql.jsp is as follows: <% @ page contenttype = "text / html; charSet = GB2312"%> <% @ Page import = "java.sql. *"%>
<% class.forname ("com.sybase.jdbc.sybdriver). NewInstance (); string url =" JDBC: Sybase: TDS: localhost: 5007 / tsdata "; // tsdata for your database name / rproperties sysprops = system.getproperties (); sysprops.put (" user "," userid "); sysprops.put (" password "," user_password "," User_Password "); Connection conn = DriverManager.getConnection (url, SysProps); 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 is successful, congratulations for you / ");% %