Home >Database >Mysql Tutorial >How to connect to MySQL database using JSP
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”
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();
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:
2. Click "System DSN", the interface is as shown
3. Click Add, and the "Create New Data Source" dialog box will appear, as shown in Figure
4. Select MySql odbc 5.1
##5. Fill in the database information
6. Click OK to return to the "ODBC Data Source Manager" dialog box, the newly created data source
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!