java - IDEA tomcat連線資料庫出現問題?
伊谢尔伦
伊谢尔伦 2017-04-18 10:53:49
0
1
913

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)
伊谢尔伦
伊谢尔伦

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

全部回覆(1)
小葫芦

原因:MySQL在高版本需要指明是否進行SSL連線

解決:

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

問題:Unknown system variable 'lower_case_table_names'

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板