Home > Database > Mysql Tutorial > How to connect to MySQL database using JSP

How to connect to MySQL database using JSP

黄舟
Release: 2017-08-03 10:44:02
Original
2642 people have browsed it

The previous article introduced the connection to the access database. This time it introduces the connection to the MySql database. This database is easier to use than access.

Connect through the MySql database driver

1.

①driverClass=”com.mysql.jdbc.Driver”
②url=”jdbc:mysql://127.0.0.1:3306/mytest”
Copy after login

How to connect to MySQL database using JSP

2. For example

Related statements to connect to the database query table:

  Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mytest","root","");
   Statement stmt=conn.createStatement();
   ResultSet rs=stmt.executeQuery("select * from userinfo");
   while(rs.next())
   {
   out.print("<br>用户名:"+rs.getString("username")+"密码:"+rs.getString("password"));
   }
   rs.close();
   stmt.close();
conn.close();
Copy after login

How to connect to MySQL database using JSP

Connect via JDBC-ODBC bridge driver

1. First set up the odbc data source. The specific steps are:

Open the control panel, "Performance and Maintenance -" Management Tools - "Data Source (ODBC)", open the data source, as shown in the figure:

How to connect to MySQL database using JSP

2. Click "System DSN", the interface is as shown

How to connect to MySQL database using JSP

3. Click Add, and the "Create New Data Source" dialog box will appear, as shown in Figure

How to connect to MySQL database using JSP

4. Select MySql odbc 5.1

How to connect to MySQL database using JSP

##5. Fill in the database information

How to connect to MySQL database using JSP

6. Click OK to return to the "ODBC Data Source Manager" dialog box, the newly created data source

How to connect to MySQL database using JSP

7 appears in the system data source. The corresponding code is

classDriver=”sun.jdbc.odbc.JdbcOdbcDriver”
url=”jdbc:odbc:MySql”
Copy after login

Give me an example

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection conn=DriverManager.getConnection("jdbc:odbc:MySql","","");
   Statement stmt=conn.createStatement();
   ResultSet rs=stmt.executeQuery("select * from userinfo");
   while(rs.next())
   {
   out.print("<br>用户名:"+rs.getString("username")+"密码:"+rs.getString("password"));
   }
   rs.close();
   stmt.close();
   conn.close();
Copy after login

The above is the detailed content of How to connect to MySQL database using JSP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template