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
- <%@ page contenttype="text/html; charset=gb2312 "%>
- <%@ page language="java"%>
- <%@ page import="Com.mysql.jdbc.Driver"%>
- <%@ page import="java.sql.*"%>
- <%
- Load Driver
- String drivername="Com.mysql.jdbc.Driver";
- Database Information
- String username="root";
- Password
- String userpasswd="123";
- Database name
- String dbname="Student";
- Table name
- String tablename="Stu_info";
- Connect the database information string to a full URL (or directly to a URL, and separate write is clear maintainability is strong)
- String url="jdbc:mysql://localhost/" +dbname+"user=" +username+"&password=" +USERPASSWD;
- Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
- Connection conn=drivermanager.getconnection (URL);
- Statement stmt = Conn.createstatement ();
- String sql="select * from" +TABLENAME;
- ResultSet rs = stmt.executequery (SQL);
- Out.print ("id");
- Out.print ("|");
- Out.print ("name");
- Out.print ("|");
- Out.print ("phone");
- Out.print ("<br>");
- while (Rs.next ()) {
- Out.print (rs.getstring (1) +"");
- Out.print ("|");
- Out.print (Rs.getstring (2) +"");
- Out.print ("|");
- Out.print (Rs.getstring (3));
- Out.print ("<br>");
- }
- Out.print ("<br>");
- Out.print ("OK, Database Query successd! ");
- Rs.close ();
- Stmt.close ();
- Conn.close ();
- %>
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