PreparedStatement类型中的方法setInt(int,int)不适用于参数(int,String)

时间:2015-12-02 22:38:01

标签: mysql jsp jdbc

我必须将我的网页链接到jsp文件,但我收到了一些错误。我有点中级btw。我有一个MySQL数据库,我打算链接我在我的网页上填写的表单,但我得到这个错误(PreparedStatement类型中的方法setInt(int,int)不适用于参数(int,String) )或(无法连接到数据库) 有人可以帮忙吗?

jsp代码:

<%@ page import="java.sql.*"%>
<html>
<head>
<title>AutoServe</title>
</head>

<body>
<h1>form</h1>

<%


String first_name = request.getParameter("fname");
String last_name = request.getParameter("lname");
String email_form = request.getParameter("email");
String phone_form = request.getParameter("phone");
String address_form = request.getParameter("address");
String year_form = request.getParameter("car_year");
String make_form = request.getParameter("make");
String model_form = request.getParameter("model");
String colour_form = request.getParameter("colour");

int car_year = Integer.parseInt(year_form);



try {

java.sql.Connection cn;
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/webpage", "root", "passwordlivi");


PreparedStatement pstatement = null;
int updateQuery = 0;

String queryString = "INSERT INTO appointment(fname, lname, email, phone, address, car_year, make, model, colour) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";


          pstatement = cn.prepareStatement(queryString);
                      pstatement.setString(1, first_name);
                      pstatement.setString(2, last_name);
                      pstatement.setString(3, email_form);
                      pstatement.setString(4, phone_form);
                      pstatement.setString(5, address_form);
                      pstatement.setInt(6, year_form);
                      pstatement.setString(7, make_form);
                      pstatement.setString(8, model_form);
                      pstatement.setString(9,    colour_form);



          updateQuery = pstatement.executeUpdate();

                        if (updateQuery != 0) { %>
         <br>
               <TABLE style="background-color: #E3E4FA;" 
               WIDTH="30%" border="1">
                  <tr><th>Data is inserted successfully 
                in database.</th></tr>
               </table>
          <%
          }
        } 
        catch (Exception ex) {
        out.println("Unable to connect to database.");

           }





%>

</body>
</html>

我的数据库代码

<%@ page import="java.sql.*"%>
<html>
<head>
<title>JDBC Connection example</title>
</head>

<body>
<h1>AutoServe.Inc</h1>

<%

java.sql.Connection cn;
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection  ("jdbc:mysql://localhost:3306/webpage", "root", "passwordlivi");


ResultSet rst=null;
Statement stmt=null;

stmt=cn.createStatement();
rst=stmt.executeQuery("select * from appointment ");



while(rst.next()){

%>

<p>
First Name: <%=rst.getString(1)%>  <br/>
Last Name: <%=rst.getString(2)%>  <br/>
Email Address: <%=rst.getString(3)%>  <br/>
Phone: <%=rst.getString(4)%>  <br/>
Address: <%=rst.getString(5)%>  <br/>
Year: <%=rst.getInt(6)%>  <br/>
Make : <%=rst.getString(7)%>  <br/>
Model: <%=rst.getString(8)%>  <br/>
Colour: <%=rst.getString(9) %> </br>


</p>

<%
}


%>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

pstatement.setInt(6, year_form);更改为pstatement.setInt(6, car_year);。您正在尝试将String放入需要整数的方法中。上面的代码已经将String转换为int。