public class Sql {
Connection cnn=null;
ResultSet rs=null;
Statement stm=null;
public void connection() {
try{
//加载注册SQLSever的JDBC驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception e) {
System.out.println("驱动加载失败");
}
try{
cnn=DriverManager.getConnection("jdbc:mysql://localhost:3306/A?serverTimezone=GMT&useSSL=false","root","123456");
stm=cnn.createStatement();
}catch(Exception e){
System.out.println("连接失败");
}
}
错误提示:Exception Report
Message An exception occurred processing JSP page [/浏览.jsp] at line [49]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing JSP page [/浏览.jsp] at line [49] 46: </form> 47: <% 48: }if(s.equalsIgnoreCase("browse")){ 49: ResultSet rs= sql.query("select * from SI order by Sage desc "); 50: out.print("<br>"); 51: out.print("<center>"); 52: out.print("<h1>瀛︾敓淇℃伅琛�</h1>"); 上面的代码连接数据库是没有任何问题的,但是我们可以看到还是会提示nullpointerException,问题就出在数据库服务没有开启,驱动加载之后找不到对应的数据库端口,所以会报错:解决方法:
同时按下“Win+X”弹出系统快捷菜单,在菜单中找到“计算器管理->服务与应用管理->服务->MySQL,然后右键开启服务
然后数据库连接就完成,可以做CURD了。