package com.bjsxt;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcTest {
//向Departments表中添加一条数据
public void instertDepartments(String department_name,int location_id) {
Connection conn=null;
Statement state=null;
try {
//驱动注册
Class.forName("com.mysql.jdbc.Driver");
//创建链接
conn=DriverManager.getConnection("jdbc:musql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8","root", "root");
String sql="insert into departments values(default,'"+department_name+"',"+location_id+")";
state=conn.createStatement();
int flag=state.executeUpdate(sql);
System.out.println(flag);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(state !=null) {
try {
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn !=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
JdbcTest test=new JdbcTest();
test.instertDepartments("研发部", 8);
}
}
报错提示:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
java.sql.SQLException: No suitable driver found for jdbc:musql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at com.bjsxt.JdbcTest.instertDepartments(JdbcTest.java:18)
at com.bjsxt.JdbcTest.main(JdbcTest.java:47)
我的代码和视频中的一摸一样,怎么报错呢??