问题描述
首先说明,数据库连接地址用户名都是对的!就是老是抛出异常?mysqlCon.java:packageDemo;importjava.sql.*;publicclassmysqlCon{privatefinalStringuName="root";privatefinalStringuPwd="gaoshuai";privatefinalStringdb="tree";privateStringurl="http://localhost:3306/"+db;privatefinalStringdriver="com.mysql.jdbc.Driver";publicResultSetrs=null;privateStatementst=null;privateConnectioncon=null;publicmysqlCon(){try{Class.forName(this.driver);con=DriverManager.getConnection(this.url,this.uName,this.uPwd);st=con.createStatement();}catch(Exceptione){e.printStackTrace();}finally{try{if(con!=null)con.close();if(st!=null)st.close();if(rs!=null)rs.close();}catch(Exceptione){e.printStackTrace();}}}publicResultSetexQuery(Stringsql){returnthis.st.executeQuery(sql);}}异常:java.lang.Error:Unresolvedcompilationproblem:UnhandledexceptiontypeSQLExceptionDemo.mysqlCon.exQuery(mysqlCon.java:41)Demo.servletMysql.doGet(servletMysql.java:30)javax.servlet.http.HttpServlet.service(HttpServlet.java:689)javax.servlet.http.HttpServlet.service(HttpServlet.java:802)调用方式:在servlet里面调用的importjava.sql.*;importDemo.mysqlCon;publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse){mysqlCondb=newmysqlCon();ResultSetgetRs=db.exQuery("selectnamefromtreewherefid=0");}
解决方案
解决方案二:
privateStringurl="http://localhost:3306/"+db;url搞错了,mysql的不是这样子的。楼主搜下mysqljdbcurl,我懒得找了。
解决方案三:
引用1楼crazylaa的回复:
privateStringurl="http://localhost:3306/"+db;url搞错了,mysql的不是这样子的。楼主搜下mysqljdbcurl,我懒得找了。
url=jdbc:mysql://localhost:3306/mydata