Jsp-using pure Java Database Driver (SQL Server, Oracle database as an example)

Source: Internet
Author: User
1. first, download the sqlserver database driver and the Oracle database driver. I will use sqljdbc below. jar and ojdbc6.jar, Here sqljdbc. jar is a driver package of SQLServer2005, and ojdbc6.jar is a database driver package of Oracle11g. It can be found in the Oracle installation directory. My

1. first, download the sqlserver database driver and the Oracle database driver. I will use sqljdbc below. jar and ojdbc6.jar, Here sqljdbc. jar is a driver package of SQLServer 2005 and ojdbc6.jar is a database driver package of Oracle 11g. It can be found in the Oracle installation directory. My

1. First download sqlserver Database Driver ProgramAnd Oracle Database Driver ProgramI will use sqljdbc. jar and ojdbc6.jar. Here, sqljdbc. jar belongs to SQL Server 2005. Driver ProgramPackage, ojdbc6.jar is Oracle 11g Database Driver ProgramPackage, which can be found in the Oracle installation directory. The installation directory is on the E disk, that is:

Zookeeper

E: \ app \ Administrator \ product \ 11.2.0 \ dbhome_1 \ jdbc \ lib, which contains some jar packages. ojdbc6.jar is used for jdk 1.6, I used jdk 1.6 directly. If not, I can download it online:


2. We can downloadDatabaseOfDriverProgramAfter the jar package, open our MyEclipse software tool and create a new project. Here, my project name is JdbcTest.DriverProgramThe package is copied to the lib directory under the WEB-INF under WebRoot, in the creation of a tom. servlet package, create 2 classes, inherit from the HttpServlet class, directory level as shown in, where JdbcTest is to test sqlserverDriverProgramJdbctest is used to test Oracle.DatabaseDriverProgramOf:

3. Start to write the test code to check whether the code can be connected.DatabaseDriverDirectly add the code. First, the code of the JdbcTest. java file is used to test sqlserver.DriverProgramOf which 127.0.0.1 is the meaning of the local server, 1433 of which is sqlserverDatabaseThe port number in TCP/IP:

Package tom. servlet; import java. io. IOException; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class JdbcTest extends HttpServlet {/** javaDriverTest */@ Overrideprotected void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// LoadDriverTry {Class. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver "). newInstance ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} // specifiesDatabaseCreate a connection try {String uri = "jdbc: sqlserver: // 127.0.0.1: 1433; DatabaseName = employee"; String user = "xg"; String password = "123456 "; connection con = DriverManager. getConnection (uri, user, password); System. out. println (con. toString ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

4. The following code is attached to the javasjdbctest. java file to test Oracle.DatabaseDriverProgramIn the following code, 127.0.0.1 indicates the local server, and 1521 indicates Oracle.DatabaseThe orcl is the global port number.DatabaseName, scott is a mode, 123456 is the password:

Package tom. servlet; import java. io. IOException; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class extends jdbctest extends HttpServlet {/** javaDriverTest */@ Overrideprotected void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// LoadDriverTry {Class. forName ("oracle. jdbc. driver. oracleDriver ");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // specifiesDatabaseCreate a Connection try {Connection con = DriverManager. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl", "scott", "123456"); System. out. println (con. toString (); con. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

5. in the web. configure the declaration of two servlets in xml, that is, under the configuration of the above two test codes, entering different URLs will display different connection success or failure information in the console, web. the xml file code is as follows:

 
   
    
      
   
    index.jsp
     
      
    
   
    jdbcTest
     
   
    tom.servlet.JdbcTest
     
      
    
   
    jdbcTest
     
   
    /test
     
        
    
   
    oracjdbcTest
     
   
    tom.servlet.OracJdbcTest
     
      
    
   
    oracjdbcTest
     
   
    /test1
     
  
 

6. Deploy this project, open the tomcat server, open the MyEclipse web Brower browser, enter their respective URLs, and click Run to view the connection failure information in the console:

The connection to sqlserver is successfully tested. Next, test Oracle.DatabaseDriverProgramIf the connection is successful, enter another URL:

Indicates connecting to OracleDatabaseDriverSucceeded.

7. Note: Pay attention to the connectionDriverPort Number problem, and whether the corresponding service is enabled, such as the TCP/IP in SQL Server may be disabled, we need to enable, or the port number is not 1433, So it depends on the situation, the default port number of sqlserver TCP/IP is 1433DatabaseThe port number is 1521. If the server is different, the connection fails.DatabaseAll of my servers are local servers and I will write 127.0.0.1 directly.

8. This article has been learned by me. It is for your reference only. It is not well written. Please forgive me. If you have any questions or errors, please point out. Thank you!

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.