请问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();
}}
还有再请大家帮我看看下面的小程序,到底哪错了,找了很长时间了!就是链接有问题,我在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();
}}
解决方案 »
- 两次循环,每次循环做一件事快,还是一次循环做两件事快
- htmlparser问题
- login.do转到UserLoginAction.java然后在UserLoginAction中通过topicList.do找到TopicListAction
- 请问哪位高手有免费的java混淆器呀?
- 大哥门帮忙给我看一下啊,我是个笨蛋,看来不适合学java
- ****为什么我的log4j配置不生效?
- 谁有e_book :core java2 第五版 中文版
- 刚学JAVA,分有的是,问题也简单!
- 环境变量的问题!!!!!!!!
- 如何将java程序变成可以脱离jdk1.4环境的可执行文件
- 关于哈弗曼译码算法 注意是译码
- 多线程问题,生产者和消费者问题
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)
异常信息写得很清楚了。。