请问oracle中的date数据类型,再用jdbc存储的时候应该怎样设置值呢?谢谢!
还有再请大家帮我看看下面的小程序,到底哪错了,找了很长时间了!就是链接有问题,我在tomcat lib文件夹中也考了驱动包了//这个是连接池的类
package com.mjn.db;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.naming.*;
import javax.sql.DataSource;public class DBConnection {

public Connection getConnection() {
Connection conn = null;
try {
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:/comp/env/jdbc/oracle");
conn = ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}

public void closeConnection(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void closePreparedStatement(PreparedStatement pstmt) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void close(ResultSet rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}//这个是bean类
package com.mjn.bean;import java.io.Serializable;
import java.sql.Date;public class Affiche implements Serializable {

private static final long serialVersionUID = 5086881106696473940L;

private int id;

private String title;

private String content;

private Date adate; public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public Date getAdate() {
return adate;
} public void setAdate(Date adate) {
this.adate = adate;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
}}//这个是dao实现类
package com.mjn.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;import com.mjn.bean.Affiche;
import com.mjn.db.DBConnection;public class AfficheDaoImpl implements AfficheDao { public boolean delete(int id) throws Exception {
DBConnection dbc = new DBConnection();
Connection conn = null;
conn = dbc.getConnection();
String sql = "delete from affiche where id=?";
boolean flag = false;
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int i = pstmt.executeUpdate();

dbc.closePreparedStatement(pstmt);
dbc.closeConnection(conn);

if(i > 0) {
flag = true;
}
return flag;
} public boolean insert(Affiche affiche) throws Exception {
DBConnection dbc = new DBConnection();
Connection conn = null;
conn = dbc.getConnection();
String sql = "insert into person(name,password)values(?,?,?)";
boolean flag = false;
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, affiche.getTitle());
pstmt.setString(2, affiche.getContent());
pstmt.setDate(3, affiche.getAdate());
int i = pstmt.executeUpdate();

dbc.closePreparedStatement(pstmt);
dbc.closeConnection(conn);

if(i > 0) {
flag = true;
}
return flag;
} public List<Affiche> queryAll() throws Exception {
Connection conn = null;
DBConnection dbc = new DBConnection();
conn = dbc.getConnection();
String sql = "select * from person";
PreparedStatement pstmt = conn.prepareStatement(sql);
List<Affiche> list = new ArrayList<Affiche>();
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
Affiche affiche = new Affiche();
affiche.setTitle(rs.getString(1));
affiche.setContent(rs.getString(2));
affiche.setAdate(rs.getDate(3));
list.add(affiche);
}
System.out.println(list.size());
dbc.close(rs);
dbc.closePreparedStatement(pstmt);
dbc.closeConnection(conn);
return list;
}}//这个是测试连接的,请问这里怎么设置affiche.setAdate()呢?
package com.mjn.dao;import java.util.List;import com.mjn.bean.Affiche;public class Test { public static void insert() {
Affiche affiche = new Affiche();
affiche.setTitle("");
}

public static void delete() {
try {
AfficheDao dao = new AfficheDaoImpl();
boolean flag = dao.delete(1);
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}

}

public static void queryAll() {

}

public static void main(String[] args) {

Test.delete();
}}

解决方案 »

  1.   

    使用的是tomcat的服务器吗?tomcat服务器要把数据库连接的驱动包放到全局的lib库目录中。。
      

  2.   

    我已经拷过lib里的驱动了,这些是异常信息javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at com.mjn.db.DBConnection.getConnection(DBConnection.java:17)
    at com.mjn.dao.AfficheDaoImpl.delete(AfficheDaoImpl.java:17)
    at com.mjn.dao.Test.delete(Test.java:17)
    at com.mjn.dao.Test.main(Test.java:31)
    java.lang.NullPointerException
    at com.mjn.dao.AfficheDaoImpl.delete(AfficheDaoImpl.java:20)
    at com.mjn.dao.Test.delete(Test.java:17)
    at com.mjn.dao.Test.main(Test.java:31)
      

  3.   

     java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(Unknown Source) 
    异常信息写得很清楚了。。