jsp连接MySQL数据库的简单方法

下面一起来了解下jsp连接MySQL数据库的简单方法,相信大家看完肯定会受益匪浅,文字在精不在多,希望jsp连接MySQL数据库的简单方法这篇短内容是你想要的。

如果是使用eclipse将该包置于:
项目->WebContent->WEB-INF->lib下即可。

连接MySQL数据库代码:

<%@ 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>

看完jsp连接MySQL数据库的简单方法这篇文章后,很多读者朋友肯定会想要了解更多的相关内容,如需获取更多的行业信息,可以关注我们的行业资讯栏目。