Home > Java > javaTutorial > body text

This article teaches you how to connect jsp to MySQL database

云罗郡主
Release: 2018-10-23 17:53:01
forward
4012 people have browsed it

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>
Copy after login

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!

Related labels:
source:csdn.net
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