- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 netbeans 和 java DB 将数据从表单输入到数据库。我的问题是我不知道如何将用户在表单中输入的数据传递到数据库。
我尝试创建一个实体类并通过它连接它,但根据我遵循的指南,我最终可以访问整个数据库而不仅仅是表单
编辑:这是一个学校项目,至少我还没有学过 JDBC。
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Registration</title>
<h:outputStylesheet name="css/jsfcrud.css"/>
<h:outputStylesheet library="css" name="styles.css"/>
</h:head>
<h:body>
<h3>Registration Page</h3>
<h:form>
<h:panelGrid columns="3"
columnClasses="rightalign,leftalign,leftali
gn">
<h:outputLabel value="Category: " for="category"/>
<h:selectOneMenu id="category" label="Category"
value="#{registrationBean.
category}" >
<f:selectItem itemLabel="Simple." itemValue="Simple"/>
<f:selectItem itemLabel="Suite." itemValue="Suite"/>
<f:selectItem itemLabel="Luxurious" itemValue="Luxurious"/>
</h:selectOneMenu>
<h:message for="category"/>
<h:outputLabel value="People: " for="people"/>
<h:selectOneMenu id="people" label="People"
value="#{registrationBean.
people}" >
<f:selectItem itemLabel="Mr." itemValue="1"/>
<f:selectItem itemLabel="Mrs." itemValue="2"/>
<f:selectItem itemLabel="Miss" itemValue="3"/>
<f:selectItem itemLabel="Miss" itemValue="4"/>
</h:selectOneMenu>
<h:message for="people"/>
<h:outputLabel value="Duration:" for="duration"/>
<h:inputText id="duration" label="Duration"
required="true"
value="#{registrationBean.duration}" />
<h:message for="duration" />
<h:panelGroup/>
<h:commandButton id="register" value="Register"
action="confirmation" />
</h:panelGrid>
</h:form>
<br />
<h:link outcome="/room/List" value="Show All Room Items"/>
</h:body>
</html>
@Entity
public class Room implements Serializable {
@Id
@Basic(optional = false)
@NotNull
@Column(nullable = false)
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(nullable = false, length = 20)
private String category;
@Basic(optional = false)
@NotNull
@Column(nullable = false)
private int people;
@Basic(optional = false)
@NotNull
@Column(nullable = false)
private int duration;
private static final long serialVersionUID = 1L;
@Id
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Integer getPeople() {
return people;
}
public void setPeople(Integer people) {
this.people = people;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Room)) {
return false;
}
Room other = (Room) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.ensode.jsf.hotelreservations.Room[ id=" + id + " ]";
}
public Room() {
}
public Room(Integer id) {
this.id = id;
}
public Room(Integer id, String category, int people, int duration) {
this.id = id;
this.category = category;
this.people = people;
this.duration = duration;
}
}
public class RoomJpaController implements Serializable {
public RoomJpaController(UserTransaction utx, EntityManagerFactory emf) {
this.utx = utx;
this.emf = emf;
}
private UserTransaction utx = null;
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Room room) throws RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
em.persist(room);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Room room) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
room = em.merge(room);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = room.getId();
if (findRoom(id) == null) {
throw new NonexistentEntityException("The room with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
Room room;
try {
room = em.getReference(Room.class, id);
room.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The room with id " + id + " no longer exists.", enfe);
}
em.remove(room);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public List<Room> findRoomEntities() {
return findRoomEntities(true, -1, -1);
}
public List<Room> findRoomEntities(int maxResults, int firstResult) {
return findRoomEntities(false, maxResults, firstResult);
}
private List<Room> findRoomEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Room.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Room findRoom(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Room.class, id);
} finally {
em.close();
}
}
public int getRoomCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Room> rt = cq.from(Room.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
这是 persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="HotelReservationsPU" transaction-type="JTA">
<jta-data-source>java:app/roomintro</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
结果是得到一个表单,虽然它可以工作,但它不会在数据库中插入任何数据。
最佳答案
亲爱的哈利斯·洛伦萨托斯:
在普通的 java 项目中,为了将数据存储在数据库中,您可以使用 Hibernate 或其他实现 JPA 规范的框架。
通常您将使用 Maven 项目,并且在 pom.xml 文件中您有一些 Hibernate 依赖项,如下所示:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.core.version}</version>
</dependency>
通常您会存储整个对象
您在此处放置的代码:
utx.begin();
em = getEntityManager();
em.persist(room);
utx.commit();
看起来您正在使用一些 JPA 实现。但为了理解您的问题,查看 pom.xml 或 hibernate.properties 等配置文件将有所帮助。
您可以上传完整的代码以便更好地理解。Y与你分享一些我学习Hibernate并将数据存储在mysql数据库中的项目 https://github.com/marianocali/concesionario
这是一个使用 Hibernate 的项目的简单示例,您可以在其中看到与您的项目具有所有配置的类似代码。
文件 persistence.xml 创建 Java 类和数据库表之间的映射。
编辑:您的 persistence.xml 文件必须包含有关数据库连接的信息:看看这两个例子:https://github.com/marianocali/concesionario/blob/master/src/main/resources/META-INF/persistence.xml
https://gist.github.com/halyph/2990769
您必须将所有这些信息添加到您的文件中。
如果这对您有帮助,请为答案投票:)如果您需要更多帮助,请告诉我
问候。
马里亚诺
关于java - 如何将 xhtml 表单中的数据插入到 netbeans 中的 Java DB 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56450461/
默认情况下,netbeans 将其设置存储在用户主目录下名为 .netbeans 的目录中。是否可以更改此目录的位置(尤其是在 Windows 下)? 感谢 James Schek,我现在知道了答案(
我每天都在使用 Netbeans IDE,突然我的“项目”和"file"选项卡消失了,我真的不知道该怎么办,我在谷歌上搜索过,但找不到任何合适且有效的解决方案来解决这个问题。 有人能帮助我吗? 如果您
我正在使用 Netbeans 来满足各种开发需求。但是安装大量插件会使 Netbeans 非常慢。是否可以为每个特定的开发需求(如 Eclipse)安装多个 Netbeans? 最佳答案 还有另一种方
只是一个简单的问题,我在哪里可以下载旧版本的 Netbeans? Netbeans 6.9.1 对 Java EE 开发人员来说尤为重要。 我看过他们的网站,但我只能找到 7.0.1 和 7.1 的下
我是新的 NetBeans 用户。当我加载我的项目时,一些文件被淡化了。当我将光标放在它们上时,它们显示为“忽略”。为什么?所有的 JAR 文件和类文件也会被忽略。 最佳答案 检查Tools->Opt
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 3年前关闭。 锁定。这个问题及其答案
任何人都知道是否可以在 netbeans 中搜索和替换所有打开的文件?如果有的话,似乎也找不到选项。 我正在使用 Netbeans 6.9 PHP IDE 最佳答案 CTRL+SHIFT+H 这将打开
是否可以将条目添加到由netbeans生成的jar的manifest.mf文件中? 例如构建一个osgi包。 最佳答案 请注意,您可以通过ant任务即时创建 list ,并动态设置属性。 首先,必须更
我最近开始使用 NetBeans,但缺少一个功能使我无法完全切换到 NetBeans。我一直使用终端,从 NetBeans 窗口切换到终端窗口很痛苦。 有没有办法将终端窗口嵌入到 NetBeans I
在netbeans中调试Java程序时,我希望调试器在导致NullPointerException的行上停止,以便可以在那里检查变量。我相信我必须在该行设置的断点上使用条件,但是条件的语法是什么? 谢
当我在JavaScript中使用PHP时,Netbeans 7.0将行突出显示为语法错误 就我而言 $(document).ready(function(){ $("#rating_comp
默认情况下,netbeans 中所有打开的文件都显示在水平选项卡中,就像浏览器一样。 我希望我的选项卡垂直列出,这可能吗? 最佳答案 工具 -> 选项 -> 外观 -> 选项卡位置 关于netbean
NetBeans 中是否有颜色选择器的快捷方式? 我在 NetBeans 网站上发现了同样的查询,据说这是可能的,但我找不到如何做到这一点的实际答案。 我的意思是:当我在 CSS 文件中编辑涉及颜色的
我刚刚开始使用 NetBeans,并正在尝试了解如何: 保存时删除尾随空格。我知道你可以手动调用它,但如果你忘记了,那就很烦人了。可以在保存时自动完成吗?我希望这样做是因为它可以保持代码整洁,因为当您
我正在开发一个 netbeans 应用程序,它的主要 TopComponent 包含一个渲染 Canvas : 当我关闭应用程序(不是顶级组件)时会生成一个错误,有时这会导致在应用程序关闭后运行一个进
有没有办法在 netbeans 中进行项目范围的搜索和替换?好像应该有,但是没找到相关信息。 谢谢 最佳答案 在编辑菜单下,您应该找到在项目中替换...的菜单项 关于netbeans - 搜索并替换整
如果是,怎么做? 最佳答案 在 Netbeans 6.9 for PHP 中,转到文件->项目属性。 在“类别”中,选择“运行配置”。有很多选项可以让您的整个项目在远程服务器上运行。 关于netbea
首先,我使用的是 NetBeans IDE 7.1(内部版本 201112071828)。 我有这段代码 onload = function() { tooltip.
/* * * This is a long comment. I broke it into lines, but that made it impossible to edit without
我在 netbeans 中有一个项目,其中有一个使用 jflex 生成的 java 扫描器。因此,当我通常使用 make 文件构建它时,我使用 -Xlint 选项,即 javac -Xlint jav
我是一名优秀的程序员,十分优秀!