快速入门
在模块下新建lib文件夹,将mysql的jar包粘贴进去并右键选择添加为库,范围选择模块库
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1.注册驱动(反射)
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://127.0.0.1:3306/test";//固定写法 3306后面是要连接的数据库名
String username = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url, username, password);
//3.定义sql
String sql = "update student set name = \"Ben\" where id = 1";
//4.获取执行sql语句的对象
Statement statement = connection.createStatement();
//5.执行sql
int i = statement.executeUpdate(sql);//返回受影响的行数
System.out.println(i);
//6.释放资源
statement.close();
connection.close();
}
}