Access the database in JSP

xiaoxiao2021-03-06  135

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.

I. JSP connection Oracle8 / 8i / 9i database (with Thin mode) TestoCle.jsp as 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 SID String user to your database = "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 operation success, congratulations");%> <% rs.close ); Stmt.close (); conn.close ();%> 2, 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.sqlser.sqlserverdriver). NewInStance () ST Ring 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 is successful, congratulations");%> <% rs.close (); stmt.close (); conn.close ();%> Third, JSP connection DB2 database TESTDB2.JSP 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 you the 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 The two fields are: <% = rs.getstring (2)%> <%}%> <% out.print ("Database operation success, congratulations");%> <% rs.close (); STMT. Close (); conn.close ();%> 4, 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 = test = TestPassword"; // Testdb for your database name 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 A field content is: <% = rs.getstring (1)%> Your second field content is: <% = rs.getstring (2)%> <%}%> <% out.print ("database Successful operation, congratulations ");%> <% rs.close (); stmt.close (); conn.close ();%> 5, JSP connection access database <% @ PAGE Import = "java.sql. *" IMPORT = "java.util. *" IMPORT = "java.io. *" import = "java.text. *" ContentType = "text / html; charset = GB2312" buffer = " 20KB "%> <%! Int all, i, m_count; string odbcqury; connection odbcconn; statement odbcstmt; resultset Odbcrs; String UserName, Title, Content, Work, Email, URL, TIME, DATE; String DateTime;%> <% Try {class.Forname ("Sun.jdbc.odbc.jdbcodbcdriver");} catch (classnotfoundexception e) {Out.print ("Driver does not exist");} try {odbcconn = drivermanager.getConnection ("JDBC: ODBC: DB1 "); odbcstmt = odbcconn.createstatement (); odbcQuery =" SELECT * from Book Where DateTime> 2001- 4-26 ORDER BY DATETIME DESC "; ODBCRS = ODBCSTMT.ExecuteQuery (odbcquery); int i = 0; While (i <130) odbcrs.next (); while (odbcrs.next ()) {// * / display database The content is used to debug programs to use // int II; try {try {for (ii = 1 ;; II ) Out.print ("
Cloumn II " IS: " ODBCRS.GETSTRING (II)) ;} catch (NullPointerException e) {out.print ( "free pointer");}} catch (SQLException e) {}} odbcrs.close (); odbcstmt.close (); odbcconn.close ();

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

New Post(0)