Now, some users who have just started to get started with JSP 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 the table.
Now let's talk about how to connect to the database through JSP.
I. Getting started with JSP connect to the Oracle8/8i/9i Database in thin Mode)
Testoracle. jsp is as follows:
- <%@ PageContentType="Text/html; charset = gb2312"%>
- <%@ PageImport="Java. SQL .*"%>
- <Html>
- <Body>
- <% Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();
- StringUrl="Jdbc: oracle: thin: @ localhost: 1521: orcl";
- // Orcl is the SID of your database
- StringUser="Scott";
- StringPassword="Tiger";
- ConnectionConn=DriverManager. GetConnection (url, user, password );
- StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
- StringSQL="Select * from test";
- ResultSetRs=Stmt. ExecuteQuery (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>
2. Connecting to SQL Server 2000/databases through JSP
Testsqlserver. jsp is as follows:
- <%@ PageContentType="Text/html; charset = gb2312"%>
- <%@ PageImport="Java. SQL .*"%>
- <Html>
- <Body>
- <% Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver"). newInstance ();
- StringUrl="Jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = pubs";
- // Pubs for your database
- StringUser="Sa";
- StringPassword="";
- ConnectionConn=DriverManager. GetConnection (url, user, password );
- StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
- StringSQL="Select * from test";
- ResultSetRs=Stmt. ExecuteQuery (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>
3. Getting started with JSP to connect to the DB2 database
Testdb2.jsp is as follows:
- <%@ PageContentType="Text/html; charset = gb2312"%>
- <%@ PageImport="Java. SQL .*"%>
- <Html>
- <Body>
- <% Class. forName ("com. ibm. db2.jdbc. app. DB2Driver"). newInstance ();
- StringUrl="Jdbc: db2: // localhost: 5000/sample";
- // Sample is your database name
- StringUser="Admin";
- StringPassword="";
- ConnectionConn=DriverManager. GetConnection (url, user, password );
- StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
- StringSQL="Select * from test";
- ResultSetRs=Stmt. ExecuteQuery (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>
-
4. Getting started with JSP connection to Informix Database
Testinformix. jsp is as follows:
- <%@ PageContentType="Text/html; charset = gb2312"%>
- <%@ PageImport="Java. SQL .*"%>
- <Html>
- <Body>
- <% Class. forName ("com. informix. jdbc. IfxDriver"). newInstance ();
- StringUrl=
- "Jdbc: informix-sqli: // 123.45.67.89: 1533/TestDB: INFORMIXSERVER=Myserver;
- User=Testuser;Password=Testpassword";
- // TestDB is your database name
- ConnectionConn=DriverManager. GetConnection (url );
- StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
- StringSQL="Select * from test";
- ResultSetRs=Stmt. ExecuteQuery (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. Getting started with JSP connection to Sybase Database
Testmysql. jsp is as follows:
- <%@ PageContentType="Text/html; charset = gb2312"%>
- <%@ PageImport="Java. SQL .*"%>
- <Html>
- <Body>
- <% Class. forName ("com. sybase. jdbc. SybDriver"). newInstance ();
- StringUrl="Jdbc: sybase: Tds: localhost: 5007/tsdata";
- // Tsdata is your database name
- PropertiesSysProps=System. GetProperties ();
- SysProps. put ("user", "userid ");
- SysProps. put ("password", "user_password ");
- ConnectionConn=DriverManager. GetConnection (url, SysProps );
- StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
- StringSQL="Select * from test";
- ResultSetRs=Stmt. ExecuteQuery (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. Getting started with JSP connection to MySQL database
Testmysql. jsp is as follows:
- <%@ PageContentType="Text/html; charset = gb2312"%>
- <%@ PageImport="Java. SQL .*"%>
- <Html>
- <Body>
- <% Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
- StringUrl="Jdbc: mysql: // localhost/softforum? User = soft & password = soft1234 & useUnicode = true & characterEncoding = 8859_1"
- // TestDB is your database name
- ConnectionConn=DriverManager. GetConnection (url );
- StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
- StringSQL="Select * from test";
- ResultSetRs=Stmt. ExecuteQuery (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>
VII. Getting started with JSP connection to PostgreSQL database
Testmysql. jsp is as follows:
- <%@ PageContentType="Text/html; charset = gb2312"%>
- <%@ PageImport="Java. SQL .*"%>
- <Html>
- <Body>
- <% Class. forName ("org. postgresql. Driver"). newInstance ();
- StringUrl="Jdbc: postgresql: // localhost/soft"
- // Soft is your database name
- StringUser="Myuser";
- StringPassword="Mypassword";
- ConnectionConn=DriverManager. GetConnection (url, user, password );
- StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
- StringSQL="Select * from test";
- ResultSetRs=Stmt. ExecuteQuery (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>
Do you know more about the above JSP entry database connection methods? Hurry up and try it!
- A brief overview of embedded objects in JSP
- Functions and principles of several encodings in JSP and Servlet
- Analysis of JSP Design Mode
- What is JSP and its strong weakness?
- How Tomcat improves performance on JSP pages