这需要使用 DriverManager.getConnection( ) 方法来创建一个 Connection 对象,它代表一个物理连接的数据库
每次操作完成后关闭所有与数据库交互的通道
e.g.:
代码示例如下:
import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//2.注册驱动
//Class.forName("com.mysql.cj.jdbc.Driver");//反射方式加载驱动类
DriverManager.registerDriver(new Driver());
//3.建立与数据库的连接
String url = "jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai";
String user = "root";
String password = "root";//写自己数据库的密码
Connection connection = DriverManager.getConnection(url,user,password); //com.mysql.cj.jdbc.ConnectionImpl@25359ed8
//4.发送sql
Statement statement = connection.createStatement(); //com.mysql.cj.jdbc.StatementImpl@442675e1
statement.executeUpdate("insert into major(name)value('数学')");
//5.关闭与数据库的连接
statement.close();
connection.close();
}
}
练习代码如下:
import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo2 {
public static void main(String[] args) throws SQLException {
//Demo2.insert("小明","男","2003-3-4","1533555",1.65);
//Demo2.update(4,"小红","女","2003-3-4","15333333",1.65);
Demo2.delete("0 or 1=1");
}
public static void insert(String name,String gender,String birthday,String phone,double height) throws SQLException {
//注册驱动
DriverManager.registerDriver(new Driver());
String url = "jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai";
String user = "root";
String password = "root";//写自己数据库的密码
//建立与数据库的连接
Connection connection = DriverManager.getConnection(url,user,password);
//发送sql
Statement statement = connection.createStatement();
statement.executeUpdate("insert into student(name,gender,birthday,phone,height)value('"+name+"','"+gender+"','"+birthday+"','"+phone+"',"+height+")");
//关闭与数据库的连接
statement.close();
connection.close();
}
public static void update(int number,String name,String gender,String birthday,String phone,double height) throws SQLException {
//注册驱动
DriverManager.registerDriver(new Driver());
String url = "jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai";
String user = "root";
String password = "root";//写自己数据库的密码
//建立与数据库的连接
Connection connection = DriverManager.getConnection(url,user,password);
//发送sql
Statement statement = connection.createStatement();
statement.executeUpdate("update student set name='"+name+"',gender='"+gender+"',birthday='"+birthday+"',phone='"+phone+"',height="+height+" where number="+number);
//关闭与数据库的连接
statement.close();
connection.close();
}
public static void delete(String number) throws SQLException {
//注册驱动
DriverManager.registerDriver(new Driver());
String url = "jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai";
String user = "root";
String password = "root";//写自己数据库的密码
//建立与数据库的连接
Connection connection = DriverManager.getConnection(url,user,password);
//发送sql
Statement statement = connection.createStatement();
statement.executeUpdate("delete from student where number= "+number); //直接将字符串拼接到sql中,不能防止sql注入攻击" 0 or 1=1"
//关闭与数据库的连接
statement.close();
connection.close();
}
}