Now have a lot of novice JSP users will often ask the database how to connect ah, how old mistakes ah? So I concentrated in this write an article for your reference, in fact, this database logic all put in the JSP may not be a good practice, but is conducive to beginners to learn, so I did so, when you learn a certain degree of time, you can consider using the pattern of MVC development. When practicing this code, you must put the JDBC driver into the server classpath, and then build a table test in the database, with two fields such as Test1, Test2, which can be built with the following SQL
CREATE TABLE test (test1 varchar, test2 varchar
and then write a Test record to the table, now start our JSP and database tour.
One, JSP connection oracle8/8i/9i database (in thin mode)
Testoracle.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 is the SID for your database
String user= "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 contains: <%=rs.getstring (2)%>
<%}%
<%out.print ("Successful database operation, congratulations");%>
<%rs.close ();
Stmt.close ();
Conn.close ();
%>
Two, JSP connection SQL server7.0/2000 database
testsqlserver.jsp 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 the
String user= "sa" for your database;
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)%>
The contents of your second field are: <%=rs.getstring (2)%>
<%}$False $
%>
<%out.print ("Successful database operation, congratulations");%>
<%rs.close ();
Stmt.close ();
Conn.close ();
%>
</body>
three, JSP connection DB2 database
testdb2.jsp is as follows:
<%@ page contenttype= "text/html;charset=gb2312″%>
<%@ page import= "java.sql.*"%>
<body>
<%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)%>
the contents of your second field are: <%=rs.getstring (2)%>
<%}%>
<%out.print ("Successful database operation, congratulations");%>
<%rs.close ();
Stmt.close ();
Conn.close ();
%>
</body>
Four, JSP connection Informix database
testinformix.jsp is as follows:
<%@ page contenttype= "text/html;charset=gb2312″%>
<%@ page import= "java.sql.*"%>
<body>
<%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 for your database name
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)%>
the contents of your second field are: <%=rs.getstring (2)%>
<%}%>
<%out.print ("Successful database operation, congratulations");%>
<%rs.close ();
Stmt.close ();
Conn.close ();
%>
</body>
Five, JSP connect Sybase database
testmysql.jsp are as follows:
<%@ page contenttype= "text/html;charset=gb2312″%>
<body>
<%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,resultset.concur_updatable);
String sql= "SELECT * from Test";
ResultSet rs=stmt.executequery (SQL);
while (Rs.next ()) {%>
Your first field content is: <%=rs.getstring (1)%>
the contents of your second field are: <%=rs.getstring (2)%>
<%}%>
<%out.print ("Successful database operation, congratulations");%>
<%rs.close ();
Stmt.close ();
Conn.close ();
%>
</body>
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 for your database name
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)%>
The contents of your second field are: <%=rs.getstring (2)%>
<%}%>
<%out.print ("Successful database operation, 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"
database name of
//soft
String user= "MyUser";
String password= "MyPassword";
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)%>
the contents of your second field are: <%=rs.getstring (2)%>
<%}%>
<%out.print ("Successful database operation, congratulations");%>
<%rs.close ();
stmt.close ();
conn.close ();
%>