用占位符和數組來操作數據庫總是提示下角標越界,找了半天也沒弄明白哪里有問題,這個地方是跟著云課堂老師的講解做的,只不過操作的數據表不一樣,但是老師那個就沒問題,我就出了問題
,如果有大神能幫忙看看,真的感激不盡!!!!
下面是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 編輯
分享至: