JSP connect SQL Server database tutorial

Source: Internet
Author: User
Tags tomcat server

First, let me start with the tool version I used for reference:

Jar Package: Jtds1.3.1.jar: Click to enter

Database: SQL Server2012

Server: Tomcat8.0: Click to enter

Developing IDE Tools: MyEclipse CI6

jdk:jdk1.8: Click to enter

PC System: Windows Education Edition 2016 anniversary update.

Two, will download the Good jar package (after downloading the decompression package inside a jar package), placed in the Tomcat root directory in the Lib directory, as shown in:

Third, start MyEclipse, create a new Web project, start the Tomcat server, and deploy the project to the server. (The jar package must be copied to the Lib directory before starting the server)

Iv. create a new JSP file in the Webroot directory and write the Java code of the connection database to the body tag in the JSP file:

<%String Dbdriver= "Net.sourceforge.jtds.jdbc.Driver"; String Dburl= "JDBC:JTDS:SQLSERVER://127.0.0.1:1433/database name"; String DBUSER= "Login Name"; String PASSWORD= "Login Password"; Try{class.forname (dbdriver); Connection cn=drivermanager.getconnection (Dburl,dbuser,password); Statement St=cn.createstatement (); String SQL= "Select field 1, Field 2, Field 3, Field 4, field 5 from table name"; ResultSet RS=st.executequery (SQL);  while(Rs.next ()) {String sno=rs.getstring ("Field 1"); String sname=rs.getstring ("Field 2"); String Sex=rs.getstring ("Field 3"); Java.sql.Date Birthday=rs.getdate ("Field four"); String SD=rs.getstring ("Field 5"); Out.print (Sno+ "," +sname+ "," +sex+ "," +birthday+ "," +sd+ "<br>"); } rs.close ();//Close Result setCn.close ();//Close Operation        }        Catch(Exception ex) {System.out.println (Ex.getmessage ()); System.out.println ("Connection Exception");        Ex.printstacktrace (); }     %>

Be careful to reverse the package in the JSP:

Import= "java.util.*,java.sql.*" pageencoding= "UTF-8"%>

Common errors and Troubleshooting 1:

The load driver could not find the class:

Workaround:

If you completed this tutorial step or reported the error, it is recommended that you restart the server or redeploy the project.

Alternatively, add the Jtds.jar package directly to the JDK's JRE library and restart the server in MyEclipse operations such as:

Or, add the jar package in the Lib under the web-inf of the Webroot directory under the Web project, and then restart the server.

JSP connect SQL Server database tutorial

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.