gpt4 book ai didi

java - 如何将用户的 'firstname' 添加到 cassandra,在尝试添加数据时获取未知标识符名字

转载 作者:行者123 更新时间:2023-11-30 07:07:46 26 4
gpt4 key购买 nike

这是尝试添加到 cassandra 时显示的错误。尝试允许用户输入用户名、密码和名字,然后该数据存储在 cassandra 中。

HTTP Status 500 - Unknown identifier firstname

类型异常报告

message Unknown identifier firstname

description The server encountered an internal error that prevented it from fulfilling this request.

异常

com.datastax.driver.core.exceptions.InvalidQueryException: Unknown identifier firstname
com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:35)
com.datastax.driver.core.DefaultResultSetFuture.extractCauseFromExecutionException(DefaultResultSetFuture.java:258)
com.datastax.driver.core.AbstractSession.prepare(AbstractSession.java:79)
uk.ac.dundee.computing.aec.instagrim.models.User.RegisterUser(User.java:40)
uk.ac.dundee.computing.aec.instagrim.servlets.Register.doPost(Register.java:56)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

根本原因

com.datastax.driver.core.exceptions.InvalidQueryException: Unknown identifier firstname
com.datastax.driver.core.Responses$Error.asException(Responses.java:97)
com.datastax.driver.core.SessionManager$1.apply(SessionManager.java:154)
com.datastax.driver.core.SessionManager$1.apply(SessionManager.java:129)
com.google.common.util.concurrent.Futures$1.apply(Futures.java:713)
com.google.common.util.concurrent.Futures$ChainingListenableFuture.run(Futures.java:861)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:724)

用于注册用户的Servlet

@WebServlet(name = "Register", urlPatterns = {"/Register"})
public class Register extends HttpServlet {
Cluster cluster=null;
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
cluster = CassandraHosts.getCluster();
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
String firstName=request.getParameter("firstname");

User us=new User();
us.setCluster(cluster);
us.RegisterUser(username, password, firstName);

response.sendRedirect("/Instagrim");

}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}

尝试插入数据库的方法。

 public boolean RegisterUser(String username, String Password, String firstName){
AeSimpleSHA1 sha1handler= new AeSimpleSHA1();
String EncodedPassword=null;
try {
EncodedPassword= sha1handler.SHA1(Password);
}catch (UnsupportedEncodingException | NoSuchAlgorithmException et){
System.out.println("Can't check your password");
return false;
}
Session session = cluster.connect("instagrim");
PreparedStatement ps = session.prepare("insert into userprofiles (login,password,firstname) Values(?,?,?)");

BoundStatement boundStatement = new BoundStatement(ps);
session.execute( // this is where the query is executed
boundStatement.bind( // here you are binding the 'boundStatement'
username,EncodedPassword,firstName));
//We are assuming this always works. Also a transaction would be good here !

return true;
}

用于用户数据的 Java Bean。

public class ProfileBean {
private String login = null;
private String first_name = null;
private String last_name = null;
private String Email = null;
private ByteBuffer pImage = null;
private int length;
private String type;
private java.util.UUID UUID = null;

public void ProfileBean(){

}
public void setLogin(String login){
this.login = login;
}
public String getLogin(){
return login;
}

public void setFirstname(String firstName){
this.first_name = firstName;
}
public String getFirstname(){
return first_name;
}
public void setLastname(String lastName){
this.last_name = lastName;
}
public String getLastname(){
return last_name;
}
public void setEmail(String Email){
this.Email = Email;
}
public String getEmail(){
return Email;
}
public void setUUID(java.util.UUID UUID){
this.UUID = UUID;
}
public String getUUID(){
return UUID.toString();
}
public void setProfilePic(ByteBuffer pImage, int length, String type)
{
this.pImage = pImage;
this.length = length;
this.type = type;
}
public ByteBuffer getBuffer(){
return pImage;
}
public int getLength(){
return length;
}
public String getType(){
return type;
}
public byte[] getBytes(){
byte image[] = Bytes.getArray(pImage);
return image;
}
}

获取用户信息的JSP代码。

  <h3>Register as user</h3>
<form method="POST" action="Register">
<ul>
<li>User Name <input type="text" name="username"></li>
<li>Password <input type="password" name="password"></li>
<li>First Name<input type="text" name="firstname"></li>
</ul>
<br/>
<input type="submit" value="Register">
</form>

创建 Cassandra 表的代码。

String CreateUserProfile = "CREATE TABLE if not exists instagrim.userprofiles (\n"
+ " login text PRIMARY KEY,\n"
+ " password text,\n"
+ " firstname text,\n"
+ " lastname text,\n"
+ " email text,\n"
+" picid uuid, \n"
+ " addresses map<text, frozen <address>>\n"
+ " );";

最佳答案

首先,在尝试运行 CREATE 语句之前,请确保您的 address UDT 存在。

其次,这在驱动程序中默默地失败了。不知道为什么,但我无法让 CREATE 真正给我一个异常(exception)。

第三,删除 \n,因为它们将作为表定义的一部分发送到 Cassandra。 Cassandra 不需要它们,事实上,它们会导致 CREATE 失败。我能够成功:

String createUserProfile = "CREATE TABLE if not exists aaron.userprofiles ("
+ " login text PRIMARY KEY,"
+ " password text,"
+ " firstname text,"
+ " lastname text,"
+ " email text,"
+ " picid uuid);";

第四,不要这样做。如果您提前通过 cqlsh(甚至 DevCenter)CREATE 您的应用程序将有更好的成功机会。

关于java - 如何将用户的 'firstname' 添加到 cassandra,在尝试添加数据时获取未知标识符名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39791509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com