A simple JSP connection MySQL use instance

Source: Internet
Author: User
Tags file copy stmt

First, the SOFTWARE environment

Download and install MYSQL,TOMACAT,JDBC, MyEclipse, or other Ides.

Second, the Environment configuration

After configuring its environment variables, download Java-specific connection to MySQL driver package jdbc, some people will find in some downloaded JDBC compressed package found in some files, For example: Mysql-connector-java-gpl-5.1.34.msi installation file, then what is this file for, and what is the difference between it and Mysql-connector-java-5.1.34-bin.jar? In fact, all two are the same, But Mysql-connector-java-gpl-5.1.34.msi is the Mysql-connector-java-5.1.xx-bin.jar encapsulated in the inside, MSI will have a folder after installation, there will be Mysql-connector-jav A-5.1.34-bin.jar This file, here we only need this mysql-connector-java-5.1.xx-bin.jar on the line.

Copy the Mysql-connector-java-x.x.x-bin.jar to the Tomcat installation bin directory D:\Tomcat 6.0\lib (if you are installing an MSI file, he may be in the Tools folder of the MySQL installation directory , this we do not recommend this use, directly download the jar file copy in the past, and then add D:\Tomcat 6.0\lib\mysql-connector-java-x.x.x-bin.jar inside the classpath. Copy this step is to configure the driver for the JSP connection database. If you are using Tomcat that comes with MyEclipse, copy the jar file directly to the "Project \webroot\web-inf\lib" path.

The purpose of this configuration is to have Java application find the driver to connect to MySQL.

third, JSP connection MySQL

Set up database student, set up table Stu_info. Now is the attempt to connect MySQL with JSP.
Build test Page test.jsp

[Java]View PlainCopy
  1. <%@ page contenttype="text/html; charset=gb2312 "%>
  2. <%@ page language="java"%>
  3. <%@ page import="Com.mysql.jdbc.Driver"%>
  4. <%@ page import="java.sql.*"%>
  5. <%
  6. Load Driver
  7. String drivername="Com.mysql.jdbc.Driver";
  8. Database Information
  9. String username="root";
  10. Password
  11. String userpasswd="123";
  12. Database name
  13. String dbname="Student";
  14. Table name
  15. String tablename="Stu_info";
  16. Connect the database information string to a full URL (or directly to a URL, and separate write is clear maintainability is strong)
  17. String url="jdbc:mysql://localhost/" +dbname+"user=" +username+"&password=" +USERPASSWD;
  18. Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
  19. Connection conn=drivermanager.getconnection (URL);
  20. Statement stmt = Conn.createstatement ();
  21. String sql="select * from" +TABLENAME;
  22. ResultSet rs = stmt.executequery (SQL);
  23. Out.print ("id");
  24. Out.print ("|");
  25. Out.print ("name");
  26. Out.print ("|");
  27. Out.print ("phone");
  28. Out.print ("<br>");
  29. while (Rs.next ()) {
  30. Out.print (rs.getstring (1) +"");
  31. Out.print ("|");
  32. Out.print (Rs.getstring (2) +"");
  33. Out.print ("|");
  34. Out.print (Rs.getstring (3));
  35. Out.print ("<br>");
  36. }
  37. Out.print ("<br>");
  38. Out.print ("OK, Database Query successd!   ");
  39. Rs.close ();
  40. Stmt.close ();
  41. Conn.close ();
  42. %>


Open our page in the browser side similar to: http://localhost:8080/Test/test.jsp can see the data of the query.

from:http://blog.csdn.net/kirinlau/article/details/53182789

A simple JSP connection MySQL use instance

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.