Detailed steps for using JDBC to connect to a MySQL database in a JSP

Source: Internet
Author: User

1, first enter the name of the Web project in your new project text box, and then click Next.

2, continue to click Next

3. Generate the Web. XML Deployment descriptor check box.

4, click Finish to complete the creation of the Web Project project.

5, open MyEclipse datebase Explore,

6, fill in the contents as shown in the figure, and add the corresponding database driver via add jars.

7, the test driver is connected successfully by testing driver, you need to enter a password. After success as shown

8, click Finish to complete the database connection, right click to select Open Database Connection, enter the password, and click OK

9, next you need to add MySQL database driver in the directory you created, you need to paste the corresponding MySQL database driver into the Web-inf/lib folder.

10. Click Add to build path in Build path by right-clicking Mysql-connectior-java-5.1.6.jar

11, enter the following code in the index.jsp, and configure the corresponding MySQL database data

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%@ page import= "java.sql.*"%>

<body>
<%


String Driver = "Com.mysql.jdbc.Driver";

The URL points to the database name you want to access test1

String url = "Jdbc:mysql://127.0.0.1:3306/test";

User name when MySQL is configured

String user = "root";

Java password when connecting to MySQL configuration

String Password = "111";

try {

1 Loading drivers

Class.forName (driver);

2 Connecting the database

Connection conn = drivermanager.getconnection (URL, user, password);

3 used to execute SQL statements

Statement Statement = Conn.createstatement ();

The SQL statement to execute

String sql = "SELECT * from Login";

ResultSet rs = statement.executequery (SQL);
String name = NULL;
String Mima=null;
while (Rs.next ()) {
Name = Rs.getstring ("UserName");
Mima = rs.getstring ("PassWord");
Out.println (name+ "\ t" +mima);
}
Rs.close ();
Conn.close ();
} catch (ClassNotFoundException e) {
System.out.println ("Sorry,can ' t find the driver!");
E.printstacktrace ();
} catch (SQLException e) {
E.printstacktrace ();
} catch (Exception e) {
E.printstacktrace ();
}

%>
</body>

12, so that the operation is successful, for the presence of 8080 port number is occupied, you can use the following methods to delete the corresponding process.

At the command prompt, enter Netstat-aon | findstr 8080

Find the PID of the corresponding process, assuming it is 7659 and then enter the following command

Taskkill/pid 7659/f

You can delete the corresponding process.

Detailed steps for using JDBC to connect to a MySQL database in a JSP

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.