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

时间:2016-09-30 12:25:49

标签: java html jsp servlets cassandra

这是尝试添加到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"
                + "  );";

1 个答案:

答案 0 :(得分:1)

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

其次,这在驱动程序中无声地失败。不知道为什么,但我无法让CREATE实际上给我一个例外。

第三,摆脱\n,因为他们被送到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表,那么您的应用程序将有更好的成功机会。