1、把数据库建好
CREATE TABLE `User` (
`id` int(11) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
2、插入一个数据
insert into User values(1,"Martin","12345","zjut")
3、建立一个Dynamic Web Project,目录如下:
4、showinfo.jsp,这里需要注意的是导入类:import="com.entity.User"
<%@ page language="java" contentType="text/htmlcharset=UTF-8"
pageEncoding="UTF-8" import="com.entity.User"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
User user = (User)request.getAttribute("user")
%>
(此处空一行)
id:<%=user.getId() %>
username:<%=user.getUsername() %>
password:<%=user.getPassword() %>
address:<%=user.getAddress() %>
(此处空一行)
<!--
也可以用el表达式,但是同样需要servlet先将值存放在request中,但是用el表达式的话需要在上面加上:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
如下,获取id这样写就可以
${user.getId()}
-->
</body>
</html>
5、结果如下:
try {Connection con = null//定义一个MYSQL链接对象
Class.forName("com.mysql.jdbc.Driver").newInstance()//MYSQL驱动
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root")//链接本地MYSQL
System.out.print("yes")
} catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage())
}
上面用的是jdbc如果用框架配置
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
这个用的Spring
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)