1.增
编写向数据库表增加一条记录的测试类代码(AddDemo1)
代码
package test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class AddDemo {
private static Statement statement;
private static Statement createStatement;
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 1、加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");// 反射技术,保证内存中只有一个Driver对象
// 2、创建连接
String url="jdbc:mysql://localhost:3306/jdbc?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=true";
String user="root";// 数据库用户名
String pw="123456";// 数据库密码(每个人都不一样我的是123456)
Connection conn=DriverManager.getConnection(url, user, pw);//补全快捷键ctrl+1
System.out.println(conn);//测试是否连接上mysql数据库
// 3、得到执行sql语句的对象
java.sql.Statement statement = conn.createStatement();//补全快捷键ctrl+1
// 4、执行sql语句,并返回结果
String sql="INSERT INTO student VALUES(NULL,'tom','男','tom@163.com','18888888888')";//执行语句
int i=statement.executeUpdate(sql);
// 5、处理结果
if (i > 0) {
System.out.println("添加成功");
}
// 6、关闭连接
statement.close();
conn.close();
}
}
执行结果
2.改
编写修改数据库表中一条记录的测试类代码
代码
package test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf8&useSSL=true", "root", "123456");
Statement statement = conn.createStatement();
statement.execute("INSERT INTO student VALUES(NULL,'zhangsan','男','zhangsan@163.com','16666666666')");
int i=statement.executeUpdate("UPDATE student SET sname='rose',sex='女' WHERE stuid=2");
if(i>0) {
System.out.println("修改成功!");
}
statement.close();
conn.close();
}
}
执行结果
3.查
编写查询数据库表中所有记录的测试类代码
代码
package test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.cj.xdevapi.Statement;
public class FindAllDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf8&useSSL=true", "root","123456");
java.sql.Statement statement=connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM student");
List<Student> stu = new ArrayList<Student>();
while (rs.next()) {
Student s = new Student();// 创建一个Student对象
// 封装Student对象:把每个记录的字段值赋给Student对象的成员变量
s.setStuld(rs.getInt("stuId"));
s.setSname(rs.getString("sname"));
s.setSex(rs.getString("sex"));
s.setEmail(rs.getString("email"));
s.setPhone(rs.getString("phone"));
// 把封装的Student对象添加到stu集合
stu.add(s);
}
stu.forEach(s -> System.out.println(s));
rs.close();
statement.close();
connection.close();
}
}
执行结果
4.删
编写从数据库表删除一条记录的测试类代码
代码
package test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DeleteDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf8&useSSL=true", "root", "123456");
Statement statement = connection.createStatement();
int i= statement.executeUpdate("DELETE FROM student WHERE stuid=2");
if(i>0) {
System.out.println("删除成功");
}
statement.close();
connection.close();
}
}
执行结果