JSP entry-level database connection

Source: Internet
Author: User

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:

 
  1. <%@ PageContentType="Text/html; charset = gb2312"%>
  2. <%@ PageImport="Java. SQL .*"%>
  3. <Html>
  4. <Body>
  5. <% Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();
  6. StringUrl="Jdbc: oracle: thin: @ localhost: 1521: orcl";
  7. // Orcl is the SID of your database
  8. StringUser="Scott";
  9. StringPassword="Tiger";
  10. ConnectionConn=DriverManager. GetConnection (url, user, password );
  11. StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
  12. StringSQL="Select * from test";
  13. ResultSetRs=Stmt. ExecuteQuery (SQL );
  14. While (rs. next () {%>
  15. The content of your first field is: <%= rs. getString (1) %>
  16. Your second field content is: <%= rs. getString (2) %>
  17. <%}%>
  18. <% Out. print ("database operation successful, congratulations"); %>
  19. <% Rs. close ();
  20. Stmt. close ();
  21. Conn. close ();
  22. %>
  23. </Body>
  24. </Html>

2. Connecting to SQL Server 2000/databases through JSP

Testsqlserver. jsp is as follows:

  1. <%@ PageContentType="Text/html; charset = gb2312"%>
  2. <%@ PageImport="Java. SQL .*"%>
  3. <Html>
  4. <Body>
  5. <% Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver"). newInstance ();
  6. StringUrl="Jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = pubs";
  7. // Pubs for your database
  8. StringUser="Sa";
  9. StringPassword="";
  10. ConnectionConn=DriverManager. GetConnection (url, user, password );
  11. StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
  12. StringSQL="Select * from test";
  13. ResultSetRs=Stmt. ExecuteQuery (SQL );
  14. While (rs. next () {%>
  15. The content of your first field is: <%= rs. getString (1) %>
  16. Your second field content is: <%= rs. getString (2) %>
  17. <%}%>
  18. <% Out. print ("database operation successful, congratulations"); %>
  19. <% Rs. close ();
  20. Stmt. close ();
  21. Conn. close ();
  22. %>
  23. </Body>
  24. </Html>


3. Getting started with JSP to connect to the DB2 database

Testdb2.jsp is as follows:

 
  1. <%@ PageContentType="Text/html; charset = gb2312"%>
  2. <%@ PageImport="Java. SQL .*"%>
  3. <Html>
  4. <Body>
  5. <% Class. forName ("com. ibm. db2.jdbc. app. DB2Driver"). newInstance ();
  6. StringUrl="Jdbc: db2: // localhost: 5000/sample";
  7. // Sample is your database name
  8. StringUser="Admin";
  9. StringPassword="";
  10. ConnectionConn=DriverManager. GetConnection (url, user, password );
  11. StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
  12. StringSQL="Select * from test";
  13. ResultSetRs=Stmt. ExecuteQuery (SQL );
  14. While (rs. next () {%>
  15. The content of your first field is: <%= rs. getString (1) %>
  16. Your second field content is: <%= rs. getString (2) %>
  17. <%}%>
  18. <% Out. print ("database operation successful, congratulations"); %>
  19. <% Rs. close ();
  20. Stmt. close ();
  21. Conn. close ();
  22. %>
  23. </Body>
  24. </Html>
  25.  

4. Getting started with JSP connection to Informix Database

Testinformix. jsp is as follows:

  1. <%@ PageContentType="Text/html; charset = gb2312"%>
  2. <%@ PageImport="Java. SQL .*"%>
  3. <Html>
  4. <Body>
  5. <% Class. forName ("com. informix. jdbc. IfxDriver"). newInstance ();
  6. StringUrl=
  7. "Jdbc: informix-sqli: // 123.45.67.89: 1533/TestDB: INFORMIXSERVER=Myserver;
  8. User=Testuser;Password=Testpassword";
  9. // TestDB is your database name
  10. ConnectionConn=DriverManager. GetConnection (url );
  11. StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
  12. StringSQL="Select * from test";
  13. ResultSetRs=Stmt. ExecuteQuery (SQL );
  14. While (rs. next () {%>
  15. The content of your first field is: <%= rs. getString (1) %>
  16. Your second field content is: <%= rs. getString (2) %>
  17. <%}%>
  18. <% Out. print ("database operation successful, congratulations"); %>
  19. <% Rs. close ();
  20. Stmt. close ();
  21. Conn. close ();
  22. %>
  23. </Body>
  24. </Html>

V. Getting started with JSP connection to Sybase Database

Testmysql. jsp is as follows:

 
  1. <%@ PageContentType="Text/html; charset = gb2312"%>
  2. <%@ PageImport="Java. SQL .*"%>
  3. <Html>
  4. <Body>
  5. <% Class. forName ("com. sybase. jdbc. SybDriver"). newInstance ();
  6. StringUrl="Jdbc: sybase: Tds: localhost: 5007/tsdata";
  7. // Tsdata is your database name
  8. PropertiesSysProps=System. GetProperties ();
  9. SysProps. put ("user", "userid ");
  10. SysProps. put ("password", "user_password ");
  11. ConnectionConn=DriverManager. GetConnection (url, SysProps );
  12. StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
  13. StringSQL="Select * from test";
  14. ResultSetRs=Stmt. ExecuteQuery (SQL );
  15. While (rs. next () {%>
  16. The content of your first field is: <%= rs. getString (1) %>
  17. Your second field content is: <%= rs. getString (2) %>
  18. <%}%>
  19. <% Out. print ("database operation successful, congratulations"); %>
  20. <% Rs. close ();
  21. Stmt. close ();
  22. Conn. close ();
  23. %>
  24. </Body>
  25. </Html>
Vi. Getting started with JSP connection to MySQL database

Testmysql. jsp is as follows:

 
  1. <%@ PageContentType="Text/html; charset = gb2312"%>
  2. <%@ PageImport="Java. SQL .*"%>
  3. <Html>
  4. <Body>
  5. <% Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
  6. StringUrl="Jdbc: mysql: // localhost/softforum? User = soft & password = soft1234 & useUnicode = true & characterEncoding = 8859_1"
  7. // TestDB is your database name
  8. ConnectionConn=DriverManager. GetConnection (url );
  9. StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
  10. StringSQL="Select * from test";
  11. ResultSetRs=Stmt. ExecuteQuery (SQL );
  12. While (rs. next () {%>
  13. The content of your first field is: <%= rs. getString (1) %>
  14. Your second field content is: <%= rs. getString (2) %>
  15. <%}%>
  16. <% Out. print ("database operation successful, congratulations"); %>
  17. <% Rs. close ();
  18. Stmt. close ();
  19. Conn. close ();
  20. %>
  21. </Body>
  22. </Html>
VII. Getting started with JSP connection to PostgreSQL database

Testmysql. jsp is as follows:

 
  1. <%@ PageContentType="Text/html; charset = gb2312"%>
  2. <%@ PageImport="Java. SQL .*"%>
  3. <Html>
  4. <Body>
  5. <% Class. forName ("org. postgresql. Driver"). newInstance ();
  6. StringUrl="Jdbc: postgresql: // localhost/soft"
  7. // Soft is your database name
  8. StringUser="Myuser";
  9. StringPassword="Mypassword";
  10. ConnectionConn=DriverManager. GetConnection (url, user, password );
  11. StatementStmt=Conn. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
  12. StringSQL="Select * from test";
  13. ResultSetRs=Stmt. ExecuteQuery (SQL );
  14. While (rs. next () {%>
  15. The content of your first field is: <%= rs. getString (1) %>
  16. Your second field content is: <%= rs. getString (2) %>
  17. <%}%>
  18. <% Out. print ("database operation successful, congratulations"); %>
  19. <% Rs. close ();
  20. Stmt. close ();
  21. Conn. close ();
  22. %>
  23. </Body>
  24. </Html>

Do you know more about the above JSP entry database connection methods? Hurry up and try it!

  1. A brief overview of embedded objects in JSP
  2. Functions and principles of several encodings in JSP and Servlet
  3. Analysis of JSP Design Mode
  4. What is JSP and its strong weakness?
  5. How Tomcat improves performance on JSP pages

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.