JSP connection of various databases

xiaoxiao2021-03-06  62

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 use the following SQL

Create Table Test (TEST1 VARCHAR (20), Test2 Varchar (20) and 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.OracleDriver"). NewInstance ();

String Url = "JDBC: Oracle: Thin: @localhost: 1521: Orcl";

// 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_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 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_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 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 is 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 Success, 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 names 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 Success, Congratulations");%>

<%

Rs.close ();

Stmt.close ();

CONN.CLOSE ();

%>

V. 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 is 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, 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 Success, Congratulations");%>

<%

Rs.close ();

Stmt.close ();

CONN.CLOSE ();

%>

6. 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 names 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 Success, Congratulations");%>

<%

Rs.close ();

Stmt.close ();

CONN.CLOSE ();

%>

7. 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 is your database name

String User = "myuser";

String password = "mypassword";

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 Success, Congratulations");%>

<%

Rs.close ();

Stmt.close ();

CONN.CLOSE ();

%>

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

New Post(0)