这里写自定义目录标题
mysql连接过程
- 注册驱动,加载Driver类
- 获得连接,得到connection
- 发送sql语句
- 断开资源,关闭相应连接
第一个jdbc程序
public static void main(String[] args) throws SQLException {
//引入jar包
//注册驱动
Driver driver = new com.mysql.jdbc.Driver();//创建Driver对象
//获得连接
//jdbc:mysql:// 协议
//localhost 主机 可以是ip地址
//3306 mysql监听的端口
//jdbc 连接mysql的哪个数据库
//本质就是socket连接
String url="jdbc:mysql://localhost:3306/jdbc";
//将用户名和密码放入到Properties对象
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password", "");
Connection connect = driver.connect(url, properties);
//执行sql
String sql="insert into actor values(null,'tom','男','2022-3-29','110')";
//statement用于执行静态sql语句并返回其生成的结果
Statement statement = connect.createStatement();
int rows = statement.executeUpdate(sql);
System.out.println(rows>0?"成功":"失败");
//关闭资源
connect.close();
statement.close();
}
mysql连接的五种方式
第一种
//方式1
public static void connect01() throws SQLException {
//注册驱动
Driver driver=new com.mysql.jdbc.Driver();
//获得连接
String url="jdbc:mysql://localhost:3306/jdbc";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
第二种
//方式2 使用反射动态加载,更加灵活,减少依赖性
public static void connect02() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, SQLException {
//使用反射动态加载,更加灵活,减少依赖性
//注册驱动
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Constructor<?> constructor=aClass.getConstructor();
Driver driver = (Driver)constructor.newInstance();
//获得连接
String url="jdbc:mysql://localhost:3306/jdbc";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
第三种
//方式3,利用DriverManager对Driver对象统一管理
public static void connect03() throws NoSuchMethodException, ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException, SQLException {
//利用反射加载Driver类
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Constructor<?> constructor = aClass.getConstructor();
Driver driver = (Driver)constructor.newInstance();
String url="jdbc:mysql://localhost:3306/jdbc";
String user="root";
String password="";
DriverManager.deregisterDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
第四种
//方式4,利用Class.forName自动完成注册驱动,简化代码
public static void connect04() throws ClassNotFoundException, SQLException {
//在加载Driver类时,已经完成注册
/*源码
静态代码块在类加载是执行一次,已经创建了Driver对象,并完成了注册
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
*/
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/jdbc";
String user="root";
String password="";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
第五种
//方式5:将用户名,url,密码等信息写入配置文件中
public static void connect05() throws IOException, ClassNotFoundException, SQLException {
//获取配置信息
Properties properties = new Properties();
properties.load(new FileReader("src/mysql.properties"));
String url=properties.getProperty("url");
String user=properties.getProperty("user");
String password=properties.getProperty("password");
String driver=properties.getProperty("driver");
//利用反射加载Driver类
Class.forName(driver);
//获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
技巧
推荐时使用第五种连接方式,使程序更加灵活