📅  最后修改于: 2021-01-02 12:56:32             🧑  作者: Mango
在本教程中,我们将使用GWT设置数据库连接。我们可以使用Eclipse来配置数据库。
Eclipse包含可以安装的HSQLDB插件。插件使安装易于设置。插件提供许多功能,例如启动/停止数据库,调用数据库管理器GUI。在此,以下步骤是配置步骤。
我们使用RStore加载数据,它具有以下功能:
RStore需要HSQLDB,Data Nucleus JDO,MyBatis和Hibernate库。下表列出了相关性。
Common | JDO | Hibernate | MyBatis |
---|---|---|---|
hsqldb-2.2.9.jar | jdo-api-3.0.1.jar | hibernate-commons-annotations-4.0.1.Final.jar | mybatis-3.2.0.jar |
commons-lang-2.6.jar | datanucleus-api-jdo-3.1.3.jar | hibernate-core-4.1.10.Final.jar | |
log4j-1.2.16.jar | datanucleus-core-3.1.4.jar | hibernate-jpa-2.0-api-1.0.1.Final.jar | |
datanucleus-rdbms-3.1.4.jar | jboss-logging-3.1.0.GA.jar | ||
datanucleus-enhancer-3.1.1.jar | jboss-transaction-api_1.1_spec-1.0.0.Final.jar | ||
asm-4.0.jar | antlr-2.7.7.jar | ||
dom4j-1.6.1.jar | |||
javassist-3.15.0-GA.jar |
下载:要下载上述文件,请点击以下链接:
通用: hsqldb , Common-lang , log4j 。
JDO: datanucleus , showfiles , jdo 。
休眠:休眠。
MyBatis: mybatis 。
将Mysql配置添加到GWT
LoginScreen.gwt.xml
数据库连接
DBConnection.java
public interface DBConnection extends RemoteService
{
public User authenticateUser(String user, String pass);
}
DBConnectionAsync.java
public interface DBConnectionAsync {
public void authenticateUser(String user, String pass, AsyncCallback
callback);
}
User.java(前端设计)
import com.google.gwt.user.client.rpc.IsSerializable;
public class User implements IsSerializable {
private String username;
private String password;
@SuppressWarnings("unused")
private User() {
//just here because GWT wants it.
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
MySQLConnection.java(创建Servlet)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.yourdomain.projectname.client.User;
public class MySQLConnection extends RemoteServiceServlet implements DBConnection {
private Connection conn = null;
private String status;
private String url = "jdbc:mysql://yourDBserver/yourDBname";
private String user = "DBuser";
private String pass = "DBpass";
public MySQLConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, user, pass);
} catch (Exception e) {
//NEVER catch exceptions like this
}
}
public int authenticateUser(String user, String pass) {
User user;
try {
PreparedStatement ps = conn.prepareStatement(
"select readonly * from users where
username = \"" + user + "\" AND " +
"password = \"" + pass + "\""
);
ResultSet result = ps.executeQuery();
while (result.next()) {
user = new User(result.getString(1),
result.getString(2));
}
result.close();
ps.close();
} catch (SQLException sqle) {
//do stuff on fail
}
return user;
}
}
输出量