preparedstatement

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了preparedstatement相关的知识,希望对你有一定的参考价值。

请高手指教一下,我初学,不知道究竟怎么回事,为什么会没有定义呢?
代码如下:

<%
String ID = request.getParameter("ID");
String password = request.getParameter("password");
String rpassword = request.getParameter("rpassword");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String year = request.getParameter("year");
if(year.length() ==1) year = "0"+year;
String mouth = request.getParameter("mouth");
if(mouth.length() == 1) mouth = "0"+mouth;
String day = request.getParameter("day");
if(day.length() == 1) day = "0"+day;
String date = year+"-"+mouth+"-"+day;
String description = request.getParameter("description");

Connection con = null;
PreparedStatement psm = null;
ResultSet rs = null;
try

Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/mydb";
con = DriverManager.getConnection(url,"root","000000");
psm = con.preparedStatement("insert into users values(?,?,?,?,?,?)");
psm.setString(1,ID);
psm.setString(2,password);
psm.setString(3,name);
psm.setString(4,sex);
psm.setLong(5,getDate(date));
psm.setString(6,description);
psm.executeUpdate();
session.setAttribute("user",ID);
response.sendRedirect("index.jsp");

catch(Exception e)
response.sendRedirect("register.jsp");

finally

if(rs!=null)
try rs.close();catch(Exception e)e.printStackTrace();

if(psm!=null)
try psm.close();catch(Exception e)e.printStackTrace();

if(con!=null)
try con.close();catch(Exception e)e.printStackTrace();


%>
错误提示如下:
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 38 in the jsp file: /do_register.jsp
The method preparedStatement(String) is undefined for the type Connection
35: Class.forName("com.mysql.jdbc.Driver").newInstance();
36: String url = "jdbc:mysql://localhost:3306/mydb";
37: con = DriverManager.getConnection(url,"root","000000");
38: psm = con.preparedStatement("insert into users values(?,?,?,?,?,?)");
39: psm.setString(1,ID);
40: psm.setString(2,password);
41: psm.setString(3,name);

不好意思 上次没看清楚
你方法名写错了 多了个d
应该是prepareStatement
参考技术A jsp里面写这么多脚本代码啊?如果没有学struts应该把逻辑写在servlet里面啊,都写在页面上啊。
首先你时间哪里处理的类型都不一样,在mysql里把放时间的字段应该设置成datetime类型的,然后导import java.sql.Date;的date包,如果取系统当前时间用System.currentTimeMillis()方法。你做的好像有点麻烦了,在想想吧。

PreparedStatement

/**
 * PreparedStatement:
 *  更新操作:public int executeUpdate()
 *  查询操作:public Result executeQuery()
 *  填充数据:public void setXxx(int index,值)//注意index是从1开始
 *  
 *  注意:在使用PreparedStatement操作Date数据时,使用的是java.sql.Date类,
 *  但在实际的程序之中描述的是java.util.Date类型,在java.util.Date中有三个子类
 *  Date(日期)、Time(时间)、Timestamp(日期时间),
 *    java.sql.Date类:public Date(long date)
 *    java.sql.Time类:public Time(long time)
 *    java.sql.Timestamp类:public Timestamp(long time)
 *  
 *  在java.util.Date中有一个方法可以将Date变为long
 *  getTime():返回long类型数据
 *  
 *  总结:先将java.util.Date的数据转换为long类型数据,再作为参数传入到java.sql.Date的构造方法里,即可实现转换。
 *     java.util.Date-->java.sql.Date
 *     new java.sql.Date (new Date().getTime()))
 *  
 */
 
public class Test011 {
   public static void main(String[] args) throws Exception {     
       //注册驱动
       Class.forName(Const.DBDRIVER);
       //获取连接对象
       Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","tiger");
       String sql=" insert into member(mid,name,age,birthday,note) values(myseq.nextval,?,?,?,?)";
       //创建操作数据库预编译对象
       PreparedStatement ps = conn.prepareStatement(sql);
       //填充数据,注意编号从1开始
       ps.setString(1, "tom");
       ps.setInt(2, 100);
       ps.setDate(3, new java.sql.Date(new Date().getTime()));
       ps.setString(4, "哈哈");
       int i = ps.executeUpdate();
       System.out.println(i);
  }
}

 

       /**
         * 根据name字段进行模糊查询(部分代码)
         */
        String column ="name";
        String keyWord="李";
        //"?"填充的占位符只有数据才可以使用,而对于列是无法使用的
        String sql=" select mid,name,age,birthday,note from member where "+column+" like ? ";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, "%"+keyWord+"%");
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            System.out.println(rs.getInt(1)+"、"+rs.getString(2)+"、"+rs.getInt(3)+"" +
                    "、"+rs.getDate(4)+"、"+rs.getString(5));
        }

 

以上是关于preparedstatement的主要内容,如果未能解决你的问题,请参考以下文章

如何在mysql中添加整数和Varchar(20)并存储为Varchar(20)