用占位符和数组来操作数据库总是提示下角标越界,找了半天也没弄明白哪里有问题,这个地方是跟着云课堂老师的讲解做的,只不过操作的数据表不一样,但是老师那个就没问题,我就出了问题
,如果有大神能帮忙看看,真的感激不尽!!!!
下面是BaseDao数据库操作通用类
package basedao;
/*
* 数据库操作通用类
*/
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class BaseDao {
protected Connection conn;
protected PreparedStatement ps;
protected Statement stmt;
protected ResultSet rs;
//获取数据库连接
public boolean getConnection(){
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
//读取类路径下的jdbc.properties文件
try {
InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
//加载驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:shop","shop_display","1234");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
//增删改
public int executeUpdate(String sql,Object ... params){
int updateRows = 0;
getConnection();
try {
ps=conn.prepareStatement(sql);
//填充占位符
for(int i = 0;i <= params.length;i++){
ps.setObject(i+1, params);
updateRows = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return updateRows;
}
//查询
public ResultSet executeSQL(String sql,Object[] params){
getConnection();
try {
ps=conn.prepareStatement(sql);
//填充占位符
for(int i = 0;i <= params.length;i++){
ps.setObject(i+1, params); //就是数组这里,控制台抛出的异常里面提示下角标越界,这里我在eclipse上面没有写错:params后面是有字母i的,但是不知道为什么放到论坛上后面的总是消失,编辑好几次了,一直不显示
}
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
//关闭资源
public boolean closeResource (){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
return true;
}
}
下面是DataBao接口
package data.impl;
public interface DataDao {
//添加数据
public void addData(int id, String name,int location,int leveis);
//删除数据
public void deleteData(int id);
//根据id修改数据
public void updateData(String name,int id);
//查询新闻信息
public void queryData();
}
下面是接口实现类
package data.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import basedao.BaseDao;
public class DataDaoImpl extends BaseDao implements DataDao{
//增加数据
public void addData(int id, String name,int location,int leveis){
try{
String sql = "insert into custom (id,name,location,leveis) VALUES (?,?,?,?)";
Object[] params={id,name,location,leveis};
int i = this.executeUpdate(sql,params);
if(i>0){
System.out.println("插入数据成功!");
}
}finally{
this.closeResource();
}
}
//删除数据
public void deleteData(int id){
try {
String sql = "delete from custom where id = ?";
Object[] params={id};
int i = this.executeUpdate(sql,params);
if(i>0){
System.out.println("删除数据成功!");
}
}finally{
this.closeResource();
}
}
//修改数据
public void updateData(String name,int id){
try {
String sql = "update custom set name = ? where id = ?";
Object[] params={name,id};
int i = this.executeUpdate(sql,params);
if(i>0){
System.out.println("更改数据成功!");
}
}finally{
this.closeResource();
}
}
//查询新闻信息
public void queryData(){
try {
String sql = "select * from custom";
Object[] params={ };
ResultSet rs=this.executeSQL(sql, params);
//(4)处理执行结果(ResultSet),释放资源
try {
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int location = rs.getInt("location");
int leveis = rs.getInt("leveis");
System.out.println(id + "\t" +name + "\t"+ location + "\t\t" + leveis);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}finally{
this.closeResource();
}
}
public static void main(String[] args){
DataDaoImpl ddi = new DataDaoImpl();
// ddi.addData(13,"华为",3,2);
// ddi.deleteData(13);
// ddi.updateData("万科",12);
ddi.queryData();
}
}
下面是数据库的表
上面黑体字加粗的部分eclipse里并没有写错
2016-5-3 19:36
2016-5-4 10:55
本帖最后由 遠山如黛 于 2016-5-4 10:55 编辑
分享至: