- 创建一个JAVA项目
- 然后在项目下添加文件夹,这里我命名为libs
- 将mysql-connector-java-8.0.12.jar文件复制到libs中
- 使用快捷键ctrl+alt+shift+s打开Project Structure界面
- 点击右上角的+号,选择1.JARs or directories,并选中刚才复制的mysql-connector-java-8.0.12.jar文件,OK
import java.sql.*;
public class JDBCTest {
public static void main(String[] args){
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
String user = "root";
String password="byhzg3";
Connection conn=null;
try{
conn=DriverManager.getConnection(url,user,password);
String sql="select * from student;";
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
System.out.println(rs.getString("s_id"));
}
rs.close();
ps.close();
}
catch(SQLException e){
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
}