0

我正在尝试向表中插入一条记录,我以 html 形式选择它...我可以在 jsp 中使用类似的东西吗?

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

2 回答 2

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>
于 2012-11-14T22:36:49.447 回答
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();
        }
    }
}

%>

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

但这不起作用:/

于 2012-11-15T19:26:37.673 回答