首页 > 代码库 > jsp链接MySQL

jsp链接MySQL

如下写入jsp源码。

在浏览器地址栏输入:http://localhost:8080/项目名/jsp源文件名.jsp

回车

 

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%><%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="http://www.mamicode.com/">        <title>加载JDBC驱动连接数据库</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="http://www.mamicode.com/styles.css">    -->  </head>   <body>    <%        try {            Class.forName("com.mysql.jdbc.Driver").newInstance();        //加载驱动            String url = "jdbc:mysql://localhost:3306/student";        //news为数据库名            String user = "root";        //数据库名            String password = "123";        //密码            Connection conn = DriverManager.getConnection(url, user, password);            out.println("连接数据库成功!");        }        catch (Exception e) {            out.println("无法连接数据库,失败!");            e.printStackTrace();        }     %>  </body></html>

 



jsp链接MySQL