静态的方法 不用new对象 可以直接通过类名调用 main() 类BL
非静态方法 不可以直接通过类名调用 getAA()
如果 静态的方法 调用 非静态方法
BL.main()
BL. getAA()* 错
证明:静态方法不可以调用非静态方法
假设:静态方法可以调用非静态方法
类 BL
静态方法main()-----》getAA()
非静态方法getAA()
BL.main() —>getAA()
BL. getAA() 错
# # #
package com.zhongruan;
import java.sql.*;
public class Test {
public static void main(String[] args) {
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true");
System.out.println("成功");
String sql="select * from music";
statement = connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if(resultSet!=null){
try{
resultSet.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(statement!=null){
try{
statement.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(connection!=null){
try{
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
}

被折叠的 条评论
为什么被折叠?



