JavaBean
简单的小Demo
实体类
JavaBean特定的写法
- 必须要有一个无参构造
- 属性必须私有化
- 必须有对应的get/set方法
一般用来和数据库的字段做映射 ORM;
ORM:对象关系映射
- 表–>类
- 字段–>属性
- 行记录–>对象
People类
index | name | age | address |
---|---|---|---|
1 | 老大 | 3 | 天 |
2 | 老二 | 2 | 天 |
3 | 老三 | 1 | 天 |
package com.hxl.pojo;
//实体类 我们一般都是和数据库中的表结构一一对应
public class People {
private int id;
private String name;
private int age;
private String address;
public People(int id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public People() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
<%@ page import="com.hxl.pojo.People" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//下面那句话等价于
/*
People people = new People();
people.setId();
people.setName();
people.setAge();
people.setAddress();
*/
%>
<jsp:useBean id="people" class="com.hxl.pojo.People" scope="page"/>
<%--name代表你要设的对象,property代表属性--%>
<jsp:setProperty name="people" property="id" value="4"/>
<jsp:setProperty name="people" property="name" value="老四"/>
<jsp:setProperty name="people" property="age" value="5"/>
<jsp:setProperty name="people" property="address" value="天"/>
<%--
下面就相当于<%=people.getId()%>
--%>
id:<jsp:getProperty name="people" property="id"/>
姓名:<jsp:getProperty name="people" property="name"/>
年龄:<jsp:getProperty name="people" property="age"/>
地址:<jsp:getProperty name="people" property="address"/>
</body>
</html>
谈一下在idea中连接数据库。
- 测试连接的时候如果出现
- 问题1:Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually.
- 解决:在cmd中输入mysql -hlocalhost -uroot -p回车输入密码。 继续输入show variables like’%time_zone’; 不要忘记分号,查看Value如果值为SYSTEM则需要设置时区。我用的是mysql8所以输入set time_zone = ‘+8:00’; 同样注意分号。如果其他版本可能需要在set后加global。设置完成后再次查看即可
- 问题2:The specified database user/password combination is rejected:
- 解决:重新修改一下密码就好。用最简单的修改方式,Navicat中进行修改
- 问题1:Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually.