java 删除mysql数据库中表格数据

package com.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Delete {
/**
* @param args
*/
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection ct;
try {
ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","hequnjie");
Statement st=ct.createStatement();
/*按条件删除表格中的数据
 * delete from student1 where id>=2;
 * 删除表格中所有数据,保留表格结构
 * delete from student1;
 * 删除表格
 * drop table student1;
 * */

String sqlstr=" delete from student1 where id>=2;";
st.executeUpdate(sqlstr);
System.out.println("删除成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}

经验分享 程序员 微信小程序 职场和发展