Many new JSP beginners often ask how to connect to the database. Why are there errors? Therefore, I wrote an article here for your reference. In fact, it is not a good practice to put all the database logic in JSP, but it is helpful for beginners to learn. So I did this, when you learn a certain degree, you can consider using the MVC mode for development. When practicing the code, you must put the JDBC driver in the server's class path, and then create a table test in the database. There are two fields: test1, Test2, you can use the following SQL to create
Create Table Test (test1 varchar (20), Test2 varchar (20)
Write a test record to this table. Now let's start our JSP and database journey.
I. jsp connection to oracle8/8i/9i Database (in thin Mode)
Testoracle. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();
String url = "JDBC: oracle: thin :@ localhost: 1521: orcl ";
// Orcl is 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 rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
Ii. jsp connection to SQL Server7.0/2000 database
Testsqlserver. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% 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_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
Iii. Connecting to the DB2 database using JSP
Testdb2.jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% 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_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html> (Code Lab)
4. Connecting to Informix Database Using JSP
Testinformix. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<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 is 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 rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
V. jsp connection to Sybase Database
Testmysql. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("com. Sybase. JDBC. sybdriver"). newinstance ();
String url = "JDBC: Sybase: TTL: 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_updatable );
String SQL = "select * from test ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
Vi. jsp connection to MySQL database
Testmysql. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
String url = "JDBC: mysql: // localhost/softforum? User = soft & Password = soft1234 & useunicode = true & characterencoding = 8859_1"