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