我有一个mysql数据库,我想连接到我的Java(Spring MVC)项目(我正在使用IntelliJ)。 据我了解,我在IntelliJ中定义了等同于数据库表的实体和存储库。但是,我写了一个简短的代码,它可以连接到数据库,运行sql查询并保存/打印查询结果:
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver"); // Connecting to mySql
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/theprocess?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","root");
// Connecting to our database
Statement stmt = con.createStatement();
// Object that allows executing SQL queries
ResultSet rs = stmt.executeQuery("select * from clients");
// Storing the query's result
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
// Defines which columns to print
con.close(); // Closing the connection;
}catch(Exception e){ System.out.println(e);}
}
所以我的问题是,如果我有该代码,我是否甚至需要实体和存储库? 感谢您的帮助:)