Java+myeclipse+tomcat (c) Configure MySQL and query data to be displayed in the JSP Web page

Source: Internet
Author: User

The first two articles describe how to configure MyEclipse and Tomcat to develop JSP Web sites, how to configure the servlet simple implementation form submission, this article mainly describes the configuration of MySQL implementation database connection MyEclipse, Finally, the data in the query table is displayed in the JSP Web page. The main picture is mainly described in the article, please Haihan ~
Java+myeclipse+tomcat (i) Introduction to the configuration process and JSP Web site development
        Java+myeclipse+tomcat (ii) Configuring the servlet and simply implementing the form submission
code and MySQL:
http://pan.baidu.com/s/1jGGKJ1k


I. Configuring MySQL

First download the mysql-5.0.96-winx64, as shown in the installation process.
1. Install MySQL 5.0




2. Select manual configuration, service type, universal multifunction, and installation path




3. Set the number of database access connections to 15, Port 3306 (set URL in code), encoded as Utf-8




4. Set the default super root user name and password, and finally install successfully


two. Query MySQL

After you have successfully installed MySQL 5.0, perform a simple database operation.
1. Run MySQL input default user password 123456


2. Create a database test01 and use a database (the second call is directly used)

3. Create table student, where the number is the primary key
4. Display the table structure, using the statement desc student
5. Insert the data into the student table and display the queried data
at this point, the MySQL operation Database Basic explanation of the end, you can also implement database additions and deletions, transactions, stored procedures and other operations, it is recommended to install visual software to replace the black box.

three. MyEclipse Query Database

To unify and simplify the Java language operations of various databases, Sun provides the JDBC framework for all Java applications to connect to the database in a unified manner. From applications such as Enterprise Oracle, DB2, SQL Server, to mid-size application MySQL, Oracle XE, and finally, access, FoxPro, and more for small personal apps. JDBC (Java database Connectivity,java connection) can be connected to any process database by using the database JDBC driver class provided by the database manufacturer.
Using the example in the previous article servlet, using JDBC to query data in a JSP, the core operations are as follows. Refer to the Hongten blog at the following address:
Http://www.cnblogs.com/hongten/archive/2011/03/29/1998311.html

1. Load the JDBC driver (MySQL driver)

Class.forName ("Com.mysql.jdbc.Driver");  
2. Provide a URL for the JDBC connection
Driver name   String drivername = "Com.mysql.jdbc.Driver";  Database user name   String userName = "root";  Password   String userpasswd = "123456";  Database name   String dbName = "test01";  Table name   String tableName = "Student";  Junction string   url = "jdbc:mysql://localhost:3306/" + DbName + "? user="          + userName + "&password=" + userpass wd
3. Creating a connection to a database
Connection Connection = drivermanager.getconnection (URL);  
4. Create a statement
To execute the SQL statement, you must obtain the Java.sql.Statement instance, which is divided into the following 3 types of statement instances:
1). Execute a static SQL statement. Typically implemented through statement instances.
2). Execute the dynamic SQL statement. Typically implemented through PreparedStatement instances.
3). Execute the database stored procedure. Typically implemented through CallableStatement instances.
5. Execute SQL statements
The statement interface provides three ways to execute SQL statements: ExecuteQuery, executeupdate, and execute
1). ResultSet executeQuery (String sqlString): Executes the SQL statement that queries the database and returns a result set (ResultSet) object.
2). int executeupdate (String sqlString): Used to execute INSERT, UPDATE, or DELETE statements, as well as SQL DDL statements such as CREATE table and drop table, etc.
3). Execute (sqlString): Used to perform statements that return multiple result sets, multiple update counts, or both.
6. Processing results
Two cases: The execution of the update returns the number of records affected by this operation, and the result returned by the execution query is a ResultSet object.
? ResultSet contains all rows that conform to the conditions in the SQL statement, and it provides access to the data in those rows through a set of Get methods.
? Get data using the access method of the result set (ResultSet) object:
This method compares efficient  columns that are numbered from left to right, and start with column 1 while (Rs.next ()) {            String name = rs.getstring ("name");        
       7. Close the JDBC Object
Release connection method con ps rspublic static void release (Connection con,statement ps,resultset rs) {try{if (rs!=null) {//close Recordset Rs.close ();} if (ps!=null) {//Close declaration ps.close ();} if (con!=null) {//Close Connection object Con.close ();}} catch (Exception e) {e.printstacktrace ();}}
you need to copy the Mysql-connector-java-5.1.15-bin.jar package file in the Project Testservlet folder Testservlet\webroot\web-inf\lib. Then modify the success.jsp code. The specific code is as follows:
<%@ page language= "java" import= "java.sql.*,java.io.*,java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <!--reference Blog http://blog.csdn.net/believejava/article/details/39111823--><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >the effect is as follows: (can refer to the second article (ii) configuration servlet and simple implementation form submission)

Finally hope that the article is helpful to you, this article is to tell the JSP connection MySQL database, the next article is ready to tell the Java file and JSP files between the operation of the database. If the article has insufficient or wrong place, also please Haihan! These four articles basically cover the basics of Java URLs, you can also implement a simple JSP site.
(By:eastmount 2015-5-12 midnight 2 o ' http://blog.csdn.net/eastmount/)

Java+myeclipse+tomcat (c) Configure MySQL and query data to be displayed in the JSP Web page

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.