在数据库连接中的 Eclipse 数据源资源管理器中,为其中的 Goolge Cloud Sql 创建一个新连接 我的数据库名称是留言簿和实例名称是 codeguestbook:appshilendra 然后在错误窗口中看到
无法更改启动配置文件的权限
在我的 jsp 页面中,我的驱动程序是“jdbc:google:rdbms://codeguestbook:appshilendra/guestbook”,用于运行项目时的谷歌云 sql 连接驱动程序
然后我在错误窗口中出现错误无法更改启动配置文件的权限
Servlet 代码是 @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
Connection c = null;
try {
DriverManager.registerDriver(new AppEngineDriver());
c = DriverManager.getConnection("jdbc:google:rdbms://codeguestbook:appshilendra/guestbook");
String fname = req.getParameter("fname");
String content = req.getParameter("content");
if (fname == "" || content == "") {
out.println("<html><head></head><body>You are missing either a message or a name! Try again! Redirecting in 3 seconds...</body></html>");
} else {
String statement ="INSERT INTO entries (guestName, content) VALUES( ? , ? )";
PreparedStatement stmt = c.prepareStatement(statement);
stmt.setString(1, fname);
stmt.setString(2, content);
int success = 2;
success = stmt.executeUpdate();
if(success == 1) {
out.println("<html><head></head><body>Success! Redirecting in 3 seconds...</body></html>");
} else if (success == 0) {
out.println("<html><head></head><body>Failure! Please try again! Redirecting in 3 seconds...</body></html>");
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (c != null)
try {
c.close();
} catch (SQLException ignore) {
}
} resp.setHeader("Refresh","3; url=/guestbook.jsp");
}
以及我在其中使用的jsp代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.List" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.google.appengine.api.rdbms.AppEngineDriver" %>
<!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/html; charset=ISO-8859-1">
<title>Database Connection</title>
</head>
<body>
<%
Connection c = null;
c = DriverManager.getConnection("jdbc:google:rdbms://codeguestbook:appshilendra /guestbook");
ResultSet rs = c.createStatement().executeQuery("SELECT guestName, content, entryID FROM entries"); %>
<table style="border: 1px solid black">
<tbody>
<tr>
<th width="35%" style="background-color: #CCFFCC; margin: 5px">Name</th>
<th style="background-color: #CCFFCC; margin: 5px">Message</th>
<th style="background-color: #CCFFCC; margin: 5px">ID</th>
</tr> <%
while (rs.next()){
String guestName = rs.getString("guestName");
String content = rs.getString("content");
int id = rs.getInt("entryID"); %>
<tr>
<td><%= guestName %></td>
<td><%= content %></td>
<td><%= id %></td>
</tr>
<% }
c.close(); %>
</tbody>
</table>
<br />
</body>
</html>