Home  >  Article  >  Java  >  This article teaches you how to connect jsp to MySQL database

This article teaches you how to connect jsp to MySQL database

云罗郡主
云罗郡主forward
2018-10-18 14:23:273938browse

What this article brings to you is an article that teaches you how to connect jsp to a MySQL database? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

If you are using eclipse, place the package in:
Just go to Project->WebContent->WEB-INF->lib.

Connect to MySQL database code:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<body>
<% 
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/mydata";
String user = "root";
String password = "root";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
Statement statement = conn.createStatement();
String sql = "select * from userInfo";
ResultSet rs = statement.executeQuery(sql);
String name = null;
String mima=null;
while (rs.next()) { 
 out.print("<br>用户名:"+rs.getString("username")+"密码:"+rs.getString("password"));
}   
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>

The above is an article to teach you how to connect jsp to MySQL database? The full introduction of this article is compact. I hope you can gain something. For more JSP video tutorials, please pay attention to the PHP Chinese website.

The above is the detailed content of This article teaches you how to connect jsp to MySQL database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete