Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何添加用户';s';名字';对于cassandra,在尝试添加数据时获取未知标识符firstname_Java_Html_Jsp_Servlets_Cassandra - Fatal编程技术网

Java 如何添加用户';s';名字';对于cassandra,在尝试添加数据时获取未知标识符firstname

Java 如何添加用户';s';名字';对于cassandra,在尝试添加数据时获取未知标识符firstname,java,html,jsp,servlets,cassandra,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 req

这是尝试添加到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;
}
用户数据的javabean

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"
                + "  );";
String CreateUserProfile=“如果不存在创建表instagrim.userprofiles(\n”
+登录文本主键,\n
+密码文本,\n
+名文本\n
+lastname文本\n
+电子邮件文本\n
+picid uuid\n
+“地址映射\n”
+ "  );";

首先,在尝试运行
CREATE
语句之前,请确保您的
地址存在

其次,这是在驱动程序内部无声地失败。不知道为什么,但我无法让
CREATE
实际返回异常

第三,去掉
\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)创建表,您的应用程序将有更好的成功机会。

您将得到一个“无效查询异常”,这似乎表明您的表中没有名为
firstname的列。你能发布你的
CREATE TABLE
语句吗?是的,现在发布。我删除了\n,但是我仍然得到了异常。注释掉了与名字有关的所有内容,奇怪的是,它只允许我使用用户登录名和密码进行注册。@RhysHovell通过cqlsh检查您的表格。如果firstname列存在,则会出现另一个问题。同样,在运行时创建表通常不是一个好主意。如果可以,在cqlsh中这样做。好的,解决了它,不确定它是什么,谢谢你的帮助。