package org.zjl.preparedstatementdem01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
public class PreparedStatementDem01 {
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/zjl";
public static final String DBUSER="root";
public static final String DBPASS="mysqladmini";
public static void main(String[] args)throws Exception {
Connection conn=null;
PreparedStatement pstmt=null;
String name="鄒繼林";
String password="www.mldnjava.cn";
int age=30;
String sex="男";
String birthday="2007-08-27";
java.util.Date temp=null;
temp=new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
java.sql.Date bir=new java.sql.Date(temp.getTime());
String sql="INSERT INTO user(name,pasword,age,sex,birthday)"
+"VALUES(?,?,?,?,?)";
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
pstmt.setInt(3, age);
pstmt.setString(4, sex);
pstmt.setDate(5,bir);
pstmt.executeUpdate(); 一直報此行有錯誤。
pstmt.close();
conn.close();
}
}
塵封記憶的十月是對的,就是拼寫錯誤,pasword應該為password.有意思的是這種錯誤IDE不會指出來,但是在注釋裡面會指出來。