code074.jsp
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JDBC的使用,数据库连接</title>
</head>
<body>
<%
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String pwd = "mysql123";
Connection conn = DriverManager.getConnection(url,user,pwd);
if(conn != null)
{
out.println("数据库连接成功!");
conn.close();
}
else
{
out.println("数据库连接失败!");
}
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
%>
</body>
</html>