Home  >  Article  >  Database  >  How to connect to MySQL database using JSP

How to connect to MySQL database using JSP

黄舟
黄舟Original
2017-08-03 10:44:022586browse

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”

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();

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”

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();

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!

Statement:
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