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, two fields such as TEST1,TEST2, you can use the following SQL to create TABLE test (test1 varchar) ( , Test2 varchar (20), and then write a Test record to the table, now start our JSP and database tour.
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.*"%>
<%class.forname ("Oracle.jdbc.driver.OracleDriver"). newinstance (); String url= "Jdbc:oracle:thin: @localhost: 1521:ORCL"; ORCL for the SID of 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)%> The contents of your second field are: <%=rs.getstring (2)%> <%}%> <%out.print ("Successful database operation, 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 of 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)%> The contents of your second field are: <%=rs.getstring (2)%> <%}%> <%out.print ("Successful database operation, 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)%> The contents of your second field are: <%=rs.getstring (2)%> <%}%> <%out.print ("Successful database operation, congratulations");%> <%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.*"%>
<%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 (); %>
|
Five, JSP connection Access database
<% @page import= "java.sql.*" Import = "java.util.*" import = "java.io.*" import= "java.text.*" contenttype= "text/html; charset=gb2312" buffer= "20kb" %><% ! int all,i,m_count; String Odbcquery; Connection Odbcconn; Statement odbcstmt; ResultSet Odbcrs; String username,title,content,work,email,url,time,date; String datetime; %> <% try{ Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver"); }catch (classnotfoundexception e) {out.print ("driver does not exist"); } try{ Odbcconn = drivermanager.getconnection ("jdbc:odbc:db1"); Odbcstmt = Odbcconn.createstatement (); odbcquery= "SELECT * from book where datetime>2001-4-26 the Order by datetime DESC"; Odbcrs=odbcstmt.executequery (odbcquery); int i=0; while (i<130) Odbcrs.next (); while (Odbcrs.next ()) { //*/////////////////////////display the contents of the database used to debug the program is used// INT II; Try{ try{ for (ii=1;;; ii++) Out.print (" Cloumn" +ii+ "is:" +odbcrs.getstring (ii)); }catch (NullPointerException e) { Out.print ("null pointer"); } }catch (SQLException e) { } } Odbcrs.close (); Odbcstmt.close (); Odbcconn.close (); }catch (SQLException e) {out.print (e); } %> |