First, JSP connection oracle8/8i/9i database (with thin mode)
Testoracle.jsp is as follows:
<%@ page contenttype= "text/html;charset=gb2312"%> <%@ page import= "java.sql.*"%> <% String result = ""; Query result string
String sql = "SELECT * from Test"; SQL string
Connection string, format: "JDBC: Database driver Name: Connection mode: @ Database server IP: Port number: Database Sid" String url = "Jdbc:oracle:thin: @localhost: 1521:ORCL"; String username = "Scott"; User name String password = "Tiger"; Password
To create an Oracle database driver instance Class.forName ("Oracle.jdbc.driver.OracleDriver"). newinstance ();
Get a connection to a database Connection conn = drivermanager.getconnection (URL, username, password); To create an execution statement object Statement stmt = Conn.createstatement (); Execute SQL statement, return result set ResultSet rs = stmt.executequery (SQL);
while (Rs.next ()) { result = = "N/a first field content:" + rs.getstring (1) + "<BR>"; }
Rs.close (); Close result set Stmt.close (); Close Execution Statement Object Conn.close (); Close the connection to the database %>
<HTML> <BODY> <%=result%> </BODY> </HTML> |
Second, JSP connection SQL server7.0/2000 database
Testsqlserver.jsp is as follows
<%@ page contenttype= "text/html;charset=gb2312"%> <%@ page import= "java.sql.*"%> <% String sql = "SELECT * from Test";
Connection string, format: "JDBC: Company Name: Database driver Name://Database server IP: port number; databasename= database name" String url = "Jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs"; String username = "Scott"; String password = "Tiger";
Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"). newinstance ();
Connection conn = drivermanager.getconnection (URL, username, password); Statement stmt = conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable); ResultSet rs = stmt.executequery (SQL);
%>
<HTML> <BODY>
<% while (Rs.next ()) { %> The first field content is: <%=rs.getstrisng (1)%><br> <% }
Rs.close (); Stmt.close (); Conn.close (); %>
</BODY> </HTML> |
Third, JSP connection DB2 database
Testdb2.jsp is as follows:
<%@ page contenttype= "text/html;charset=gb2312"%> <%@ page import= "java.sql.*"%> <% string sql = "SELECT * from Test"; //Connection string, format: "JDBC: Database driver name://Database server IP: Port number/database name" String url = "Jdbc:db2://localhost:5000/sample"; string username = "Scott"; string password = "Tiger"; class.forname ("Com.ibm.db2.jdbc.app.DB2Driver"). newinstance (); connection conn = drivermanager.getconnection (URL, username, password); statement stmt = Conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable); ResultSet rs = stmt.executequery (sql); rs.close (); stmt.close (); conn.close (); %> |
Four, JSP connection Informix database
Testinformix.jsp is as follows:
<%@ page contenttype= "text/html;charset=gb2312"%> <%@ page import= "java.sql.*"%> <% string sql = "SELECT * from Test"; //Connection string, format: "JDBC: Database driver name://Database server IP: Port number/database name: informixserver= server name; user= user name; password= password " string url =" Jdbc:informix-sqli://123.45.67.89:1533/testdb:i Nformixserver=myserver;user=testuser;password=testpassword "; class.forname ("Com.informix.jdbc.IfxDriver"). newinstance (); connection conn = drivermanager.getconnection (URL); statement stmt = Conn.createstatement (resultset.type_scroll_sensitive, ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executequery (sql); rs.close (); stmt.close (); conn.close (); %> |
Five, JSP connection Sybase database
Testsybase.jsp is as follows:
<%@ page contenttype= "text/html;charset=gb2312"%> <%@ page import= "java.sql.*"%> <% String sql = "SELECT * from Test";
Connection string, format: "JDBC: Company Name: Database driver Name: Database Server IP: Port number/database name" String url = "Jdbc:sybase:tds:localhost:5007/tsdata";
Properties prop = System.getproperties (); Prop.put ("User", "userid"); User name Prop.put ("Password", "User_password"); Password
Class.forName ("Com.sybase.jdbc.SybDriver"). newinstance ();
Connection conn = drivermanager.getconnection (URL, prop); Statement stmt = conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable); ResultSet rs = stmt.executequery (SQL);
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.*"%> <% String sql = "SELECT * from Test";
Connection string, format: "JDBC: Database driver name://Database server ip/database name? user= username &password= Password & use unicode= Boolean & character encoding = Encoding" String url = "jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useunicode=true& Characterencoding=8859_1 "; String username = "Scott"; String password = "Tiger";
Class.forName ("Org.gjt.mm.mysql.Driver"). newinstance ();
Connection conn = drivermanager.getconnection (URL, username, password); Statement stmt = conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable); ResultSet rs = stmt.executequery (SQL);
Rs.close (); Stmt.close (); Conn.close (); %> |
Seven, JSP connection PostgreSQL database
Testpostgresql.jsp is as follows:
<%@ page contenttype= "text/html;charset=gb2312"%> <%@ page import= "java.sql.*"%> <% string sql = "SELECT * from Test"; //Connection string, format: "JDBC: Database driver name://Database server ip/database name" string url = "Jdbc:postgresql://localhost/soft"; string username = "Scott"; string password = "Tiger"; class.forname ("Org.postgresql.Driver"). newinstance (); connection conn = drivermanager.getconnection (URL, username, password); statement stmt = Conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable); ResultSet rs = stmt.executequery (sql); rs.close (); stmt.close (); conn.close (); %> |