81,114
社区成员
发帖
与我相关
我的任务
分享
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
try {
// 加载数据库驱动,注册到驱动管理器
Class.forName("com.mysql.jdbc.Driver");
// 数据库连接字符串
String url = "jdbc:mysql://localhost:3306/test";
// 数据库用户名
String username = "root";
// 数据库密码
String password = "111";
// 创建Connection连接
Connection conn = DriverManager.getConnection(url,username,password);
// 判断 数据库连接是否为空
if(conn != null){
// 输出连接信息
out.println("数据库连接成功!");
// 关闭数据库连接
conn.close();
}else{
// 输出连接信息
out.println("数据库连接失败!");
}
} catch (ClassNotFoundException e) {
System.out.println("找不到包!");
} catch (SQLException e) {
System.out.println("SQLException");
}
%>
</body>
</html>