求助!java,mysql数据库,执行删除操作的时候出现错误?异常?实际上数据库里已经删除了,命令行没显示
求助!java,mysql数据库,执行删除操作的时候出现错误?异常?实际上数据库里已经删除了,StudentID=3的那一行已经消失了,但是命令行不能显示出内容了???源...
求助!java,mysql数据库,执行删除操作的时候出现错误?异常?实际上数据库里已经删除了,StudentID=3的那一行已经消失了,但是命令行不能显示出内容了???
源代码:
import java.sql.*;
public class UpdateDeleteDemo {
public static void main(String[] args)throws Exception{
try{
//调用Class.forName()方法加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载MySQL驱动!");
String url="jdbc:mysql://localhost:3306/student"; //JDBC的URL
Connection conn;
conn = DriverManager.getConnection(url, "root","");
Statement stmt = conn.createStatement(); //创建Statement对象
System.out.println("成功连接到数据库!");
//查询数据的代码
String sql = "select * from stu"; //要执行的SQL
ResultSet rs = stmt.executeQuery(sql);//创建数据对象
System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
while (rs.next()){
System.out.print(rs.getInt(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.print(rs.getInt(3) + "\t");
System.out.println();
}
//修改数据的代码
System.out.println("———————————————修改—————————————————");
String sql2 = "update stu set name=? where StudentID=?";
PreparedStatement pst = conn.prepareStatement(sql2);
pst.setString(1,"lisa");
pst.setInt(2,3);
pst.executeUpdate();
ResultSet rs12 = stmt.executeQuery(sql);//创建数据对象
while (rs12.next()){
System.out.print(rs12.getInt(1) + "\t");
System.out.print(rs12.getString(2) + "\t");
System.out.print(rs12.getInt(3) + "\t");
System.out.println();
}
//删除数据的代码
System.out.println("———————————————删除—————————————————");
String sql3 = "delete from stu where StudentID=?";
pst = conn.prepareStatement(sql3);
pst.setInt(1,3);
pst.executeUpdate();
ResultSet rs2 = stmt.executeQuery(sql);//创建数据对象
System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
while (rs.next()){
System.out.print(rs2.getInt(1) + "\t");
System.out.print(rs2.getString(2) + "\t");
System.out.print(rs2.getInt(3) + "\t");
System.out.println();
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
} 展开
源代码:
import java.sql.*;
public class UpdateDeleteDemo {
public static void main(String[] args)throws Exception{
try{
//调用Class.forName()方法加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载MySQL驱动!");
String url="jdbc:mysql://localhost:3306/student"; //JDBC的URL
Connection conn;
conn = DriverManager.getConnection(url, "root","");
Statement stmt = conn.createStatement(); //创建Statement对象
System.out.println("成功连接到数据库!");
//查询数据的代码
String sql = "select * from stu"; //要执行的SQL
ResultSet rs = stmt.executeQuery(sql);//创建数据对象
System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
while (rs.next()){
System.out.print(rs.getInt(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.print(rs.getInt(3) + "\t");
System.out.println();
}
//修改数据的代码
System.out.println("———————————————修改—————————————————");
String sql2 = "update stu set name=? where StudentID=?";
PreparedStatement pst = conn.prepareStatement(sql2);
pst.setString(1,"lisa");
pst.setInt(2,3);
pst.executeUpdate();
ResultSet rs12 = stmt.executeQuery(sql);//创建数据对象
while (rs12.next()){
System.out.print(rs12.getInt(1) + "\t");
System.out.print(rs12.getString(2) + "\t");
System.out.print(rs12.getInt(3) + "\t");
System.out.println();
}
//删除数据的代码
System.out.println("———————————————删除—————————————————");
String sql3 = "delete from stu where StudentID=?";
pst = conn.prepareStatement(sql3);
pst.setInt(1,3);
pst.executeUpdate();
ResultSet rs2 = stmt.executeQuery(sql);//创建数据对象
System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
while (rs.next()){
System.out.print(rs2.getInt(1) + "\t");
System.out.print(rs2.getString(2) + "\t");
System.out.print(rs2.getInt(3) + "\t");
System.out.println();
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询