JSP connection all kinds of database Daquan

Source: Internet
Author: User
Tags db2 informix postgresql stmt sybase sybase database mysql database

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 ();

%&gt;


</body&gt;




three, JSP connection DB2 database


testdb2.jsp is as follows:


<%@ page contenttype= "text/html;charset=gb2312″%&gt;


<%@ page import= "java.sql.*"%&gt;




<body&gt;


<%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 ()) {%&gt;


Your first field content is: <%=rs.getstring (1)%&gt;


the contents of your second field are: <%=rs.getstring (2)%&gt;


<%}%&gt;


<%out.print ("Successful database operation, congratulations");%&gt;


<%rs.close ();


Stmt.close ();


Conn.close ();


%&gt;


</body&gt;




Four, JSP connection Informix database


testinformix.jsp is as follows:


<%@ page contenttype= "text/html;charset=gb2312″%&gt;


<%@ page import= "java.sql.*"%&gt;




<body&gt;


<%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 ()) {%&gt;


Your first field content is: <%=rs.getstring (1)%&gt;


the contents of your second field are: <%=rs.getstring (2)%&gt;


<%}%&gt;


<%out.print ("Successful database operation, congratulations");%&gt;


<%rs.close ();


Stmt.close ();


Conn.close ();


%&gt;


</body&gt;




Five, JSP connect Sybase database


testmysql.jsp are as follows:


<%@ page contenttype= "text/html;charset=gb2312″%&gt;




<body&gt;


<%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 ()) {%&gt;


Your first field content is: <%=rs.getstring (1)%&gt;


the contents of your second field are: <%=rs.getstring (2)%&gt;


<%}%&gt;


<%out.print ("Successful database operation, congratulations");%&gt;


<%rs.close ();


Stmt.close ();


Conn.close ();


%&gt;


</body&gt;


Six, JSP connection MySQL database


testmysql.jsp is as follows:


<%@ page contenttype= "text/html;charset=gb2312″%>


<%@ page import= "java.sql.*"%&gt;








<%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 ();
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.