java - IDEA tomcat连接数据库出现问题?
伊谢尔伦
伊谢尔伦 2017-04-18 10:53:49
[Java讨论组]

Javaweb新手,配置jndi后连接数据后总出现异常
报错出现在 myConn = ds.getConnection();

修改my.ini后还是会出现这种问题

配置jndi的context.xml文件



    
    

web.xml文件




    
        jdbc/studentinfo
        javax.sql.DataSource
        Container
    
    
    
        StudentServlet
        init.StudentServlet
    
    
        StudentServlet
        /StudentServlet
    

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("");
        out.println("StudentInfo");
        if((request.getParameter("sno") == null || request.getParameter("sno").equals("input number"))){
            out.println("
"); out.println(""); out.println("学号查询:"); out.println("
"); out.println("
"); }else if (!(request.getParameter("sno").equals("input number"))){ Vector vc = new Vector(); out.println("
"); out.println(""); out.println("学生学号:"); out.println("
"); out.println("
"); out.println(""); out.println(""); try { if((vc = accessDB(Integer.parseInt((String)request.getParameter("sno")))) != null){ out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); } }catch (Exception e){ out.println(""); out.println(""); out.println(""); out.println("数据库里没有你要查询得记录"); e.printStackTrace(); } out.println("
学号姓名语文数学
" + vc.elementAt(0) + "" + vc.elementAt(1) + "" + vc.elementAt(2) + "" + vc.elementAt(3) + "
没有记录:
"); } out.println(""); out.println(""); 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)
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(1)
高洛峰

原因:MySQL在高版本需要指明是否进行SSL连接

解决:

jdbc:mysql://localhost:3306/studentinfo?characterEncoding=utf8&useSSL=true

问题:Unknown system variable 'lower_case_table_names'

降低 mysql-connector-java jar版本试试

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号