JSP connection database Daquan

zhaozj2021-02-12  197

JSP connection database

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 write to this table to a test record so now 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 ();%>

Second, JSP connection SQL Server 7.0 / 2000 database TestsqlServer.jsp is as follows: <% @ page contenttype = "text / html; charset = GB2312"%> <% @ page import = "java.sql. *"%> <% class.forname ("com.microsoft.jdbc.sqlser.sqlserdriver). NewInstance (); string url =" JDBC: Microsoft: SQL Server: // localhost: 1433; DatabaseName = Pubs; //// pubs as 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 ();%>

Third, 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 For: <% = rs.getstring (2)%> <%}%> <% out.print ("Database Operation, Congratulations");%> <% rs.close (); stmt.close (); Conn.close ();%>

Fourth, 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 name for 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 is successful, congratulations");%> <% rs.close (); stmt.close (); conn.close () ;%>

5. JSP connection Sybase database TestMysql.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 Properties Sysprops = System. getProperties (); SysProps.put ( "user", "userid"); SysProps.put ( "password", "user_password"); Connection conn = DriverManager.getConnection (url, SysProps); Statement stmt = conn.createStatement (ResultSet .Ty_scroll_sensitive, resultset.concur_updatable; string sql = "select * from test"; resultset = 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 Success, Congratulations"); %> <% rs.close (); stmt.close (); conn.close ();%>

Sixth, JSP connection mysql database TestMysql.jsp is as follows: <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ page import = "java.sql. *"%> < .% Class.forName ( "org.gjt.mm.mysql.Driver") newInstance (); String url = "jdbc:? mysql: // localhost / softforum user = soft & password = soft1234 & useUnicode = true & characterEncoding = 8859_1" // testDB as your name 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 is successful, congratulations");%> <% rs.close (); stmt.close (); conn.close ();%>

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

New Post(0)