可以运行 但是报错
java.lang.NullPointerException at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:340) at utils.JDBCUtils.<clinit>(JDBCUtils.java:29) at day01.dao.StudentDaolmpl.findall(StudentDaolmpl.java:14) at day01.service.StudentServicelmpl.findall(StudentServicelmpl.java:14) at day01.controller.StudentController.findall(StudentController.java:15) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; public class StudentDaolmpl implements StudentDao { @Override public ArrayList<Student> findall() throws Exception { Connection con= JDBCUtils.getConnection(); Statement stat=con.createStatement(); String sql="SELECT * FROM h318"; ResultSet rs = stat.executeQuery(sql); ArrayList<Student> arry=new ArrayList<Student>(); while (rs.next()) { Integer id= rs.getInt("id"); String name=rs.getString("name"); Integer age=rs.getInt("age"); String xuehao=rs.getString("xuehao"); Student s=new Student(id,age,name,xuehao); arry.add(s); } //释放资源 JDBCUtils.close(con,stat,rs); return arry; }
package utils; //JDBC工具类的编写 import java.io.InputStream; import java.sql.*; import java.util.Properties; public class JDBCUtils { //1私有化方法 private JDBCUtils(){} //2声明所需要的配置变量 private static String diverClass; private static String url; private static String username; private static String password; private static Connection con; // 3提供静态代码块 static { try { InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties"); Properties prop=new Properties(); prop.load(is); diverClass=prop.getProperty("diverClass"); url=prop.getProperty("url"); username=prop.getProperty("username"); password=prop.getProperty("password"); Class.forName(diverClass); } catch (Exception e) { e.printStackTrace(); } } // 4提供数据库的连接方法 public static Connection getConnection(){ try { con= DriverManager.getConnection(url,username,password); } catch (Exception e) { e.printStackTrace(); } return con; } // 5提供释放资源的方法 public static void close(Connection con, Statement stat, ResultSet rs) throws SQLException { if (con!=null){ con.close(); } if (stat!=null) { stat.close(); } if (rs!=null) { rs.close(); } } public static void close(Connection con, Statement stat) throws SQLException { if (con!=null){ con.close(); } if (stat!=null) { stat.close(); } } }