![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
用JDBC-ODBC桥连接ACCESS数据库怎么插入不了数据啊但是可以查询数据 求各位大虾能帮忙
我的代码是这样的:省略连接代码*/1.插入和查询等publicclassContactDaoextendsDao{publicContactDao()throwsSQLE...
我的 代码是这样的:省略连接代码
*/
1.插入和查询等
public class ContactDao extends Dao {
public ContactDao() throws SQLException {
super();
// TODO Auto-generated constructor stub
}
public Contact selectContact(int id) throws SQLException {
String sql = "select * from tbContact where co_id=" + id;
ResultSet rs = dbhander.executaQuery(sql);
if (rs.next()) {
Contact contact = new Contact();
contact.setCo_id(rs.getInt(1));
contact.setCo_name(rs.getString(2));
contact.setCo_position(rs.getString(3));
contact.setCo_sex(rs.getString(4));
contact.setCo_age(rs.getInt(5));
contact.setCo_airPhone(rs.getString(6));
contact.setCo_officePhone(rs.getString(7));
contact.setCo_address(rs.getString(8));
contact.setCo_favor(rs.getString(9));
contact.setCo_id(rs.getInt(10));
return contact;
} else {
return null;
}
}
public int insertContact(Contact contact) throws SQLException {
String co_name1 = contact.getCo_name();
String co_position1 = contact.getCo_position();
String co_sex1 = contact.getCo_sex();
int co_age1 = contact.getCo_age();
String co_airPhone1 = contact.getCo_airPhone();
String co_officePhone1 = contact.getCo_officePhone();
String co_address1 = contact.getCo_address();
String co_favor1 = contact.getCo_favor();
int cu_id1 = contact.getCu_id();
String sql = "insert into tbContact (co_name,co_position,co_sex,co_age,co_airPhone,co_officePhone,co_address,co_favor,cu_id) values('"
+ co_name1 + "','"
+ co_position1 + "','"
+ co_sex1 + "',"
+ co_age1 + ",'"
+ co_airPhone1 + "','"
+ co_officePhone1 + "','"
+ co_address1 + "','"
+ co_favor1 + "',"
+ cu_id1 + ")";
System.out.println(sql);
try {
if (dbhander.execUpData(sql) == 1)
return 1;
else return -1;
} catch (Exception e) {
System.out.print(3);
return 2;
}
}
public int deleteContact(int co_id) throws SQLException {
String sql = "delete from tbcontact where co_id=" + co_id;
try {
dbhander.execUpData(sql);
return 1;
} catch (Exception e) {
return -1;
}
}
public int updateContact(Contact contact) throws SQLException {
String sql_1 = "update tbContact set co_name='" + contact.getCo_name()
+ "',co_position='" + contact.getCo_position();
String sql_2 = "',co_sex='" + contact.getCo_sex() + "',co_age="
+ contact.getCo_age() + ",co_airPhone='"
+ contact.getCo_airPhone();
String sql_3 = "',co_officePhone='" + contact.getCo_officePhone()
+ "',co_address='" + contact.getCo_address() + "',co_favor='"
+ contact.getCo_favor();
String sql = sql_1 + sql_2 + sql_3 + "',cu_id=" + contact.getCu_id()
+ " where co_id=" + contact.getCo_id();
try {
dbhander.execUpData(sql);
return 1;
} catch (Exception e) {
return -1;
}
}
} 展开
*/
1.插入和查询等
public class ContactDao extends Dao {
public ContactDao() throws SQLException {
super();
// TODO Auto-generated constructor stub
}
public Contact selectContact(int id) throws SQLException {
String sql = "select * from tbContact where co_id=" + id;
ResultSet rs = dbhander.executaQuery(sql);
if (rs.next()) {
Contact contact = new Contact();
contact.setCo_id(rs.getInt(1));
contact.setCo_name(rs.getString(2));
contact.setCo_position(rs.getString(3));
contact.setCo_sex(rs.getString(4));
contact.setCo_age(rs.getInt(5));
contact.setCo_airPhone(rs.getString(6));
contact.setCo_officePhone(rs.getString(7));
contact.setCo_address(rs.getString(8));
contact.setCo_favor(rs.getString(9));
contact.setCo_id(rs.getInt(10));
return contact;
} else {
return null;
}
}
public int insertContact(Contact contact) throws SQLException {
String co_name1 = contact.getCo_name();
String co_position1 = contact.getCo_position();
String co_sex1 = contact.getCo_sex();
int co_age1 = contact.getCo_age();
String co_airPhone1 = contact.getCo_airPhone();
String co_officePhone1 = contact.getCo_officePhone();
String co_address1 = contact.getCo_address();
String co_favor1 = contact.getCo_favor();
int cu_id1 = contact.getCu_id();
String sql = "insert into tbContact (co_name,co_position,co_sex,co_age,co_airPhone,co_officePhone,co_address,co_favor,cu_id) values('"
+ co_name1 + "','"
+ co_position1 + "','"
+ co_sex1 + "',"
+ co_age1 + ",'"
+ co_airPhone1 + "','"
+ co_officePhone1 + "','"
+ co_address1 + "','"
+ co_favor1 + "',"
+ cu_id1 + ")";
System.out.println(sql);
try {
if (dbhander.execUpData(sql) == 1)
return 1;
else return -1;
} catch (Exception e) {
System.out.print(3);
return 2;
}
}
public int deleteContact(int co_id) throws SQLException {
String sql = "delete from tbcontact where co_id=" + co_id;
try {
dbhander.execUpData(sql);
return 1;
} catch (Exception e) {
return -1;
}
}
public int updateContact(Contact contact) throws SQLException {
String sql_1 = "update tbContact set co_name='" + contact.getCo_name()
+ "',co_position='" + contact.getCo_position();
String sql_2 = "',co_sex='" + contact.getCo_sex() + "',co_age="
+ contact.getCo_age() + ",co_airPhone='"
+ contact.getCo_airPhone();
String sql_3 = "',co_officePhone='" + contact.getCo_officePhone()
+ "',co_address='" + contact.getCo_address() + "',co_favor='"
+ contact.getCo_favor();
String sql = sql_1 + sql_2 + sql_3 + "',cu_id=" + contact.getCu_id()
+ " where co_id=" + contact.getCo_id();
try {
dbhander.execUpData(sql);
return 1;
} catch (Exception e) {
return -1;
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询