java - IDEA tomcat连接数据库出现问题?
问题描述
Javaweb新手,配置jndi后连接数据后总出现异常报错出现在 myConn = ds.getConnection();
修改my.ini后还是会出现这种问题
配置jndi的context.xml文件
<?xml version='1.0' encoding='UTF-8'?><Context path='/' reloadable='true'> <Resource name='jdbc/studentinfo' auth='Container' type='javax.sql.DataSource' maxActive='100' maxIdle='30' maxWait='10000' username='root' password='000000' driverClassName='com.mysql.jdbc.Driver' url='jdbc:mysql://localhost:3306/studentinfo?'> </Resource></Context>
web.xml文件
<?xml version='1.0' encoding='UTF-8'?><web-app xmlns='http://xmlns.jcp.org/xml/ns/javaee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd' version='3.1'> <resource-ref><res-ref-name>jdbc/studentinfo</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth> </resource-ref><servlet><servlet-name>StudentServlet</servlet-name><servlet-class>init.StudentServlet</servlet-class> </servlet> <servlet-mapping><servlet-name>StudentServlet</servlet-name><url-pattern>/StudentServlet</url-pattern> </servlet-mapping></web-app>
java文件
public class StudentServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType('text/html;charset=GB2312');PrintWriter out = response.getWriter();out.println('<html>');out.println('<head><title>StudentInfo</title></head>');if((request.getParameter('sno') == null || request.getParameter('sno').equals('input number'))){ out.println('<form method=’post’ action=’/Servlet/StudentServlet’ >'); out.println(''); out.println('学号查询:<input type=’text’ name=’sno’ value=’input number’ />'); out.println('<input type=’submit’ value=’查询’ /><br>'); out.println('</form>');}else if (!(request.getParameter('sno').equals('input number'))){ Vector vc = new Vector(); out.println('<form method=’post’ action=’/Servlet/StudentServlet’ >'); out.println(''); out.println('学生学号:<input type’text’ name=’sno’ />'); out.println('</form>'); out.println('<br>'); out.println('<table border=’1’>'); out.println('<tr><th>学号</th><th>姓名</th><th>语文</th><th>数学</th></tr>'); try {if((vc = accessDB(Integer.parseInt((String)request.getParameter('sno')))) != null){ out.println('<tr>'); out.println('<td>' + vc.elementAt(0) + '</td>'); out.println('<td>' + vc.elementAt(1) + '</td>'); out.println('<td>' + vc.elementAt(2) + '</td>'); out.println('<td>' + vc.elementAt(3) + '</td>'); out.println('</tr>');} }catch (Exception e){out.println('<tr>');out.println('<td>没有记录:</td>');out.println('</tr>');out.println('数据库里没有你要查询得记录');e.printStackTrace(); } out.println('</table>');}out.println('</body>');out.println('</html>');out.flush();out.close(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response); } @Override public void init() throws ServletException { } public Vector accessDB(int id){Vector vc = new Vector();DataSource ds = null;Context ctx;Connection myConn = null;try { ctx = new InitialContext(); ds = (DataSource) ctx.lookup('java:comp/env/jdbc/studentinfo');}catch (Exception e){ e.printStackTrace();}if (ds == null){ System.out.println('Error');}else { System.out.println('Connection is OK!');}PreparedStatement statement = null;ResultSet mySet = null;try { myConn = ds.getConnection(); statement = myConn.prepareStatement('SELECT * FROM info WHERE sno=?'); statement.setInt(1,id); mySet = statement.executeQuery(); while (mySet.next()){vc.add(Integer.toString(mySet.getInt('sno')));vc.add(mySet.getString('sname'));vc.add(Integer.toString(mySet.getInt('chinese')));vc.add(Integer.toString(mySet.getInt('math'))); } statement.close(); mySet.close(); myConn.close(); return vc;}catch (Exception e){ e.printStackTrace();}return vc; } public static void main(String[] args) {StudentServlet aa = new StudentServlet();System.out.println(aa.accessDB(100).elementAt(1)); }}
修改context.xml的urlurl='jdbc:mysql://localhost:3306/studentinfo?useSSL=true'
还是会出现异常如下:java.sql.SQLException: Cannot create PoolableConnectionFactory (Unknown system variable ’lower_case_table_names’)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2303)at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2043)at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1543)at init.StudentServlet.accessDB(StudentServlet.java:102)at init.StudentServlet.doGet(StudentServlet.java:47)
问题解答
回答1:原因:MySQL在高版本需要指明是否进行SSL连接解决:jdbc:mysql://localhost:3306/studentinfo?characterEncoding=utf8&useSSL=true问题:Unknown system variable ’lower_case_table_names’降低 mysql-connector-java jar版本试试
相关文章:
1. 计算机专业,未毕业,自己买了一套Java视频看,打算花两个月时间,到时出去找份实习的,算是自己自学吗?2. dockerfile - 为什么docker容器启动不了?3. docker start -a dockername 老是卡住,什么情况?4. Thinkphp 下载地址找不到了?5. angular.js - angularjs的自定义过滤器如何给文字加颜色?6. javascript - iview 打包之后 找不到自带的icon图片,而且路径重复,点解7. javascript - 微信小程序 wx.downloadFile下载文件大小有限制吗8. Python 中如何对单个字典中同一个 key 的值进行合并?9. java - Dubbo接口参数序列化问题10. html5 - vue-cli 装好了 新建项目的好了,找不到项目是怎么回事?