jsp怎么连接mysql数据库代码
答案:1 悬赏:30 手机版
解决时间 2021-11-10 06:53
- 提问者网友:动次大次蹦擦擦
- 2021-11-10 01:49
jsp怎么连接mysql数据库代码
最佳答案
- 五星知识达人网友:上分大魔王
- 2021-11-10 02:05
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<%
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名test1
String url = "jdbc:mysql://127.0.0.1:3306/test";
// MySQL配置时的用户名
String user = "root";
// Java连接MySQL配置时的密码
String password = "111";
try {
// 1 加载驱动程序
Class.forName(driver);
// 2 连接数据库
Connection conn = DriverManager.getConnection(url, user, password);
// 3 用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select * from login";
ResultSet rs = statement.executeQuery(sql);
String name = null;
String mima=null;
while (rs.next()) {
name = rs.getString("userName");
mima = rs.getString("passWord");
out.println(name+"\t"+mima);
}
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();
}
%>
<%@ page import="java.sql.*" %>
<%
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名test1
String url = "jdbc:mysql://127.0.0.1:3306/test";
// MySQL配置时的用户名
String user = "root";
// Java连接MySQL配置时的密码
String password = "111";
try {
// 1 加载驱动程序
Class.forName(driver);
// 2 连接数据库
Connection conn = DriverManager.getConnection(url, user, password);
// 3 用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select * from login";
ResultSet rs = statement.executeQuery(sql);
String name = null;
String mima=null;
while (rs.next()) {
name = rs.getString("userName");
mima = rs.getString("passWord");
out.println(name+"\t"+mima);
}
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();
}
%>
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯