JDBC实现宠物信息的增效删改查
主方法:
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动
Driver();
Pet pet = new Pet(55,"小黑","公");
//添加信息
add(pet);
//删除信息
delet();
//修改信息
updata();
//普通查询
query();
}
}
宠物类:
public class Pet {
private int id;
private String name;
private String sex;
public Pet(int id, String name, String sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
加载驱动:
public static void Driver() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
String root = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url,"root","password");
Statement statement = connection.createStatement();
}
添加信息:
private static void add(Pet pet) throws SQLException {
// TODO Auto-generated method stub
//定义SQL语句
String sql="insert into pet values("+pet.getId()+",'"
+pet.getName()+"','"+pet.getSex()+"')";
System.out.println(sql);
String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
String root = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url,"root","password");
Statement statement = connection.createStatement();
int rows = statement.executeUpdate(sql);
if(rows>0) {
System.out.println("----操作成功----");
}else {
System.out.println("----操作失败----");
}
}
删除信息:
private static void delet() throws SQLException {
// TODO Auto-generated method stub
String sql="delete from pet where id = 1";
System.out.println(sql);
String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
String root = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url,"root","password");
Statement statement = connection.createStatement();
int rows = statement.executeUpdate(sql);
if(rows>0) {
System.out.println("----操作成功----");
}else {
System.out.println("----操作失败----");
}
}
修改信息:
private static void delet() throws SQLException {
// TODO Auto-generated method stub
String sql="delete from pet where id = 1";
System.out.println(sql);
String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
String root = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url,"root","password");
Statement statement = connection.createStatement();
int rows = statement.executeUpdate(sql);
if(rows>0) {
System.out.println("----操作成功----");
}else {
System.out.println("----操作失败----");
}
}
查询信息:
private static void query() throws SQLException {
// TODO Auto-generated method stub
String sql="select *from pet ";
System.out.println(sql);
String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
String root = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url,"root","password");
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
while(result.next())
{
int id = result.getInt("id");
String name = result.getString("name");
String sex = result.getString("sex");
System.out.println(id+"\t"+name+"\t"+sex+"\t");
}
}
本文详细介绍了如何使用Java JDBC实现对宠物信息的增删改查操作,包括加载数据库驱动,添加新宠物,删除宠物记录,更新宠物信息以及查询所有宠物的步骤。
1万+

被折叠的 条评论
为什么被折叠?



