JAVA 实现数据库增删改查的Dao和DaoImpl的写法
2个回答
展开全部
package org.dao;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import org.dbc.DBManageer;
import org.dbc.DBUtil;
import org.vo.CardInfo;
public class CardInfoDao {
public void testResultSetMetaData() {
String sql = "select * from cardinfo";
Connection con = new DBUtil().getConnection();
PreparedStatement ps = null;
ResultSetMetaData rsmd = null;
ResultSet rs = null;
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
rsmd = rs.getMetaData();
for (int i = 1; i < rsmd.getColumnCount(); i++) {
System.out.print(" 数据类型名:" + rsmd.getColumnClassName(i));
System.out.print(" 别名:" + rsmd.getColumnLabel(i));
System.out.print(" 列名:" + rsmd.getColumnName(i));
System.out.print(" 数据类型:" + rsmd.getColumnTypeName(i));
System.out.println(" 数据类型:" + rsmd.getColumnType(i));
}
while (rs.next()) {
// 通过反射可以对VO对象(CardInfo)自动赋值
// for(...)
// CardInfo ci=new CardInfo();
// ci.setId(rs.getInt(columnIndex))
/*
* CardInfo ca = new CardInfo(); Class cc = ca.getClass();
*/
/*Class<?> c = Class.forName("org.vo.CardInfo");
CardInfo ca = (CardInfo) c.newInstance();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);//ok */
//加载一个CardInfo类
Class c = Class.forName("org.vo.CardInfo");
Object o = c.newInstance(); //获得它的一个实例
//定义String类的对象数组
Class[] params = new Class[] { String.class};
//定义float类的对象数组
Class[] floatparams = new Class[] { float.class};
//定义Date(util)类的对象数组
Class[] dateparams = new Class[] { Date.class};
//获得setCardId方法
Method me = c.getMethod("setCardId", params);
//实例方法的参数
Object []ostr = new Object[]{rs.getString("cardId")};
//如果底层方法是静态的,那么可以忽略指定的 obj 参数。该参数可以为 null。
//如果底层方法所需的形参数为 0,则所提供的 args 数组长度可以为 0 或 null。
//o 调用方法的对象 ostr 方法调用的参数
me.invoke(o, ostr);
//获得setCustomerName方法
Method name = c.getMethod("setCustomerName", params);
Object []cusname = new Object[]{rs.getString("customerName")};
name.invoke(o,cusname);
//获得setCurrentMoney方法
Method money = c.getMethod("setCurrentMoney", floatparams);
Object []cusmoney = new Object[]{rs.getFloat("currentMoney")};
money.invoke(o,cusmoney);
//获得setOpenDate方法
Method date = c.getMethod("setOpenDate",dateparams);
Object []openDate = new Object[]{rs.getDate("openDate")};
date.invoke(o, openDate);
//打印
System.out.println(o);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void showResultSet() {
String sql = "select * from cardinfo";
Connection con = new DBManageer().getConnection();
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery();
System.out.println("---依次读取------");
while (rs.next()) {
CardInfo ca = new CardInfo();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);
}
System.out.println("---倒读------");
while (rs.previous()) {
CardInfo ca = new CardInfo();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);
}
rs.absolute(3);// 定位倒第几行
rs.updateString("customerName", "star");
// rs.updateRow();
rs.beforeFirst();
while (rs.next()) {
CardInfo ca = new CardInfo();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import org.dbc.DBManageer;
import org.dbc.DBUtil;
import org.vo.CardInfo;
public class CardInfoDao {
public void testResultSetMetaData() {
String sql = "select * from cardinfo";
Connection con = new DBUtil().getConnection();
PreparedStatement ps = null;
ResultSetMetaData rsmd = null;
ResultSet rs = null;
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
rsmd = rs.getMetaData();
for (int i = 1; i < rsmd.getColumnCount(); i++) {
System.out.print(" 数据类型名:" + rsmd.getColumnClassName(i));
System.out.print(" 别名:" + rsmd.getColumnLabel(i));
System.out.print(" 列名:" + rsmd.getColumnName(i));
System.out.print(" 数据类型:" + rsmd.getColumnTypeName(i));
System.out.println(" 数据类型:" + rsmd.getColumnType(i));
}
while (rs.next()) {
// 通过反射可以对VO对象(CardInfo)自动赋值
// for(...)
// CardInfo ci=new CardInfo();
// ci.setId(rs.getInt(columnIndex))
/*
* CardInfo ca = new CardInfo(); Class cc = ca.getClass();
*/
/*Class<?> c = Class.forName("org.vo.CardInfo");
CardInfo ca = (CardInfo) c.newInstance();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);//ok */
//加载一个CardInfo类
Class c = Class.forName("org.vo.CardInfo");
Object o = c.newInstance(); //获得它的一个实例
//定义String类的对象数组
Class[] params = new Class[] { String.class};
//定义float类的对象数组
Class[] floatparams = new Class[] { float.class};
//定义Date(util)类的对象数组
Class[] dateparams = new Class[] { Date.class};
//获得setCardId方法
Method me = c.getMethod("setCardId", params);
//实例方法的参数
Object []ostr = new Object[]{rs.getString("cardId")};
//如果底层方法是静态的,那么可以忽略指定的 obj 参数。该参数可以为 null。
//如果底层方法所需的形参数为 0,则所提供的 args 数组长度可以为 0 或 null。
//o 调用方法的对象 ostr 方法调用的参数
me.invoke(o, ostr);
//获得setCustomerName方法
Method name = c.getMethod("setCustomerName", params);
Object []cusname = new Object[]{rs.getString("customerName")};
name.invoke(o,cusname);
//获得setCurrentMoney方法
Method money = c.getMethod("setCurrentMoney", floatparams);
Object []cusmoney = new Object[]{rs.getFloat("currentMoney")};
money.invoke(o,cusmoney);
//获得setOpenDate方法
Method date = c.getMethod("setOpenDate",dateparams);
Object []openDate = new Object[]{rs.getDate("openDate")};
date.invoke(o, openDate);
//打印
System.out.println(o);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void showResultSet() {
String sql = "select * from cardinfo";
Connection con = new DBManageer().getConnection();
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery();
System.out.println("---依次读取------");
while (rs.next()) {
CardInfo ca = new CardInfo();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);
}
System.out.println("---倒读------");
while (rs.previous()) {
CardInfo ca = new CardInfo();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);
}
rs.absolute(3);// 定位倒第几行
rs.updateString("customerName", "star");
// rs.updateRow();
rs.beforeFirst();
while (rs.next()) {
CardInfo ca = new CardInfo();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询