在jsp中插入变量表名

时间:2012-11-14 21:29:54

标签: html mysql forms jsp insert

我正在尝试将一条记录插入到表中,我以html格式选择...我可以在jsp中使用这样的内容吗?

String queryString = "INSERT INTO ? (login,password,full_name,ulevel) VALUES (?, ?, ?, ?)";

2 个答案:

答案 0 :(得分:0)

主要答案 如果你想使用JDBC,我同意它可能是什么,你可以试试这个:

<% String myquery = "SELECT * FROM EMPLOYEES WHERE DEPARTMENT = ?"; %>
<% PreparedStatement mystatement = connection.prepareStatement(myquery); %>
<% mystatement.setString(1, request.getParameter("myURLparam")); %>
<% ResultSet results= mystatement.execute(); %>

您可以参考此链接了解如何使用它。 Java Oracle有更好的例子:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

上一个答案: 使用字符串格式

Java示例:

String fs;
fs = String.format("The value of the float " +
               "variable is %f, while " +
               "the value of the " + 
               "integer variable is %d, " +
               " and the string is %s",
               floatVar, intVar, stringVar);

http://docs.oracle.com/javase/tutorial/java/data/strings.html - 检查底部。

将其应用于JSP。

<html>
    <head>
      <title>Concatenate String in JSP</title>
    </head>
    <body bgcolor="#fff">
        <% String tableName = "Table"; %>
        <% String login = "login"; %>
        <% String password = "myPassword"; %>
        <% String fullName = "Full Name"; %>
        <% String ulevel = "Level 1"; %>
        <% String msg = "INSERT INTO " + tableName + " (login,password,full_name,ulevel) VALUES ("+ login + ", "+password+","+fullName+", "+ulevel+")"; %>
        <% out.println(msg); %>
    </body>
</html>

答案 1 :(得分:0)

我有:

<%
String login = request.getParameter("login");
String password = request.getParameter("password");
String full_name = request.getParameter("full_name");
String ulevel = request.getParameter("ulevel");
String team_id = request.getParameter("team_id");
String fs = String.format("insert into " + "%s " + "(login,password,full_name,ulevel) values " + "(%s,%s,%s,%s)", team_id, login, password, full_name, ulevel);

Connection connection = null;
PreparedStatement pstatement = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
int updateQuery = 0;
if (login != null && password != null && full_name != null && ulevel != null && team_id != null) {
    if (login != "" && password != "" && full_name != "" && ulevel != "" && team_id != "") {

        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/coding", "root", "root");
            pstatement = connection.prepareStatement(fs);
            pstatement.setString(1, login);
            pstatement.setString(2, password);
            pstatement.setString(3, full_name);
            pstatement.setString(4, ulevel);
            updateQuery = pstatement.executeUpdate();
            if (updateQuery != 0) {

                response.sendRedirect("index.jsp");



            }
        } catch (Exception ex) {
            out.println("Unable to connect to database.");
            } finally {
            pstatement.close();
            connection.close();
        }
    }
}

%GT;

参数login,password,full_name,ulevel,team_id来自其他源代码的html表单。

但这不起作用:/