我用Java连接数据库,try Catch ,try执行sql语句都成功了,但是catch还是抛出异常,这是怎么回事? 20
//将相关信息存入Basic表publicvoidAdd2Basic(Stringname,Stringtel,Stringemail,Stringaddress){tr...
//将相关信息存入Basic表
public void Add2Basic(String name,String tel,String email,String address){
try {
ct=new ConnDB().getConn();
//创建一个SQL语句对象
sm =ct.createStatement();
//执行SQL语句executeQuery
try {
// rs=sm.executeQuery("select * from testInfo");
// if(rs.next()){
// System.out.println("查询成功");
// }
// System.out.println("there"+rs.getString(1));
rs=sm.executeQuery("create table testInfo (name varchar(20) not null primary key ,tel char(11) ,Email varchar(50),Adrs varchar(50))");
} catch (Exception e) {
System.out.println(name+"有可能该表已经存在!");
}
System.out.println("开始删元组");
try {
rs=sm.executeQuery("delete from testInfo where name='sa'");
} catch (Exception e) {
System.out.println("删除失败");
}
System.out.println("开始将数据加入到数据库");
sm.executeQuery("insert into testInfo (name,tel,Email,Adrs) values ('"+name+"','"+tel+"','"+email+"','"+address+"')");
} catch (SQLException e) {
System.out.println("插值失败");
}finally{
this.close();
}
} 展开
public void Add2Basic(String name,String tel,String email,String address){
try {
ct=new ConnDB().getConn();
//创建一个SQL语句对象
sm =ct.createStatement();
//执行SQL语句executeQuery
try {
// rs=sm.executeQuery("select * from testInfo");
// if(rs.next()){
// System.out.println("查询成功");
// }
// System.out.println("there"+rs.getString(1));
rs=sm.executeQuery("create table testInfo (name varchar(20) not null primary key ,tel char(11) ,Email varchar(50),Adrs varchar(50))");
} catch (Exception e) {
System.out.println(name+"有可能该表已经存在!");
}
System.out.println("开始删元组");
try {
rs=sm.executeQuery("delete from testInfo where name='sa'");
} catch (Exception e) {
System.out.println("删除失败");
}
System.out.println("开始将数据加入到数据库");
sm.executeQuery("insert into testInfo (name,tel,Email,Adrs) values ('"+name+"','"+tel+"','"+email+"','"+address+"')");
} catch (SQLException e) {
System.out.println("插值失败");
}finally{
this.close();
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询