import java.sql.*;
public class E1 {
public static void main(String args[]) {
Connection con=null;
Statement sql;
ResultSet rs;
try{ Class.forName("com.mysql.cj.jdbc.Driver"); //加载JDBC_MySQL驱动
}
catch(Exception e){}
String uri = "jdbc:mysql://localhost:3306/students";
String user ="root";
String password ="13002425830";
try{
con = DriverManager.getConnection(uri,user,password); //连接代码
}
catch(SQLException e){
throw new RuntimeException(e);
}
try {
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM mess"); //查询mess表
while(rs.next()) {
String number=rs.getString(1);
String name=rs.getString(2);
int people=rs.getInt(3);
float height=rs.getFloat(4);
double weight=rs.getDouble(5);
System.out.printf("%s\t",number);
System.out.printf("%s\t",name);
System.out.printf("%s\t",people);
System.out.printf("%.2f\t",height);
System.out.printf("%.2f\n",weight);
}
con.close();
}
catch(SQLException e) {
System.out.println(e);
}
}
}
我关注你了,给你改了下代码,私聊发过去了
驱动类8.x的jar包导入了吗
我认为这篇文章有参考意义,你最好去官网下载驱动(跟你版本对应),下面文章介绍如何java如何使用驱动,我认为能解决你的问题。
java mysql 驱动_mysql jdbc驱动下载-mysql数据库JDBC驱动(mysql-connector-java)下载 v8.0.13 官方最新版
https://blog.csdn.net/weixin_32705179/article/details/113133272
可以在pom.xml中添加MySQL依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
你的mysql版本,是哪个?你的驱动是8.x的,如果你的版本更低 要重新下载对应版本
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.9</version>
</dependency>
你应该是扫了jar包,如果你的项目有pom.xml文件,你可以加上这个,如果么有的话,你需要手动去下载mysql驱动8