Various database connection code (JSP)

xiaoxiao2021-03-06  24

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.sqlserver.sqlser VERDRIVER "). 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 is successful, congratulations");%> <% rs.close (); stmt.close (); conn. Close ();%> 3, 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_updata; 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 ();%> (Code Lab) INFORMIX Database Testinformix.jsp As shown below: <% @ 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.Type_Scroll_Sensitive, Resu LTSET.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 ();%> six, 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 name for your database Connection 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 second field content is: <% = rs.getstring (2)%> <%}%> <% out.print ("Database operation is successful, congratulations");%> <% rs.close (); stmt.close (); conn.close ();%> seven, JSP connection PostgreSQL database TestMysql.jsp is as follows: <% @ Page ContentType = "Text / HTML; Charset = GB2312 "%> <% @ page import =" java.sql. * "%> <% class.forname (" org.postgreSql.driver). NewInstance (); string URL = "JDBC: postgreSQL: // localhost / soft" // Soft for your database name string user = "myuser"; string password = "MyPAS sword "; 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, congratulations ");%> <% rs.close (); stmt.close (); conn.close ();%> / * conn.jsp * / <% String sDBDriver = "COM.ibm.db2.jdbc.app.DB2Driver"; String sConnStr = "jdbc: db2: faq"; Connection conn = null; Statement stmt = null; ResultSet rs = null; try {Class.forName (sDBDriver );} Catch (java.lang.classNotfoundException e) {output ("FAQ (): E.getMessage ());} Try {conn = drivermanager.getConnection (Sconnstr," WSDemo "," WSDemo1 ") ; Stmt = conn.createstatement ();} catch (sqlexception e) {output (e.tostring ());}%>

<% @ page language = "java" import = "java.sql. *"%> <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ include file = "conn.jsp"%> <% ....... int Pages = 0; int PageSize = 10; ResultSet Result = NULL; ResultSet Rcount = NULL;

Pages = New Integer (Request.getParameter

IF (Pages> 0) {

String SQL = "State = 'I am not stupid'"; int count = 0; try {rount = stmt.executeQuery ("SELECT Count (ID) AS ID from user where" sql); Catch (SQLEXception ex) {OUT. Print ("AQ.ExecuteQuery:" EX.GETMESSAGE ());} if (ruit.next ()) count = rcount.getint ("ID"); rcount.close ();

IF (count> 0) {SQL = "Select * from user where" SQL; try {result = stmt.executeQuery (SQL);} catch (sqlexception ex) {output ("AQ.ExecuteQuery:" EX. GetMessage ());

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

New Post(0)