zoukankan      html  css  js  c++  java
  • Servlet连接数据库

    局部变量:Connetion conn=null;

                 Statement stmt=null;

                 ResultSet rs=null;

    try{

    Class.forName(“com.mysql.jdbc.Driver”);

    conn=DriverManager.getConnection(“jdbc:mysql://localhost/databasename?user=sa$password=sa;”);

    stmt=conn.createStatement();

    rs=stmt.executeQuery(“select * from tablename”);

    while(rs.Next()){

    rs.getString(“columnName”);

    }

    }catch(ClassNotFoundException ex){

    e.printStackTrace();

    }

    catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(rs != null) {
                        rs.close();
                        rs = null;
                    }
                    if(stmt != null) {
                        stmt.close();
                        stmt= null;
                    }
                    if(conn != null) {
                        conn.close();
                        conn = null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }

    注意:1.检查连接字符串。

    2.确认数据库服务器已经启动

    3.把jdbc的驱动程序拷到tomcat中。

    App的WEB-INF下的lib目录中需要相应的jar包无用。

  • 相关阅读:
    从今天起,我要在博客园装13了,听说这里装的好可以得高薪
    HTML5 选择前置摄像头,选择后置摄像头
    canvas图片base64编码后,ajax提交到后台
    238. Product of Array Except Self
    230. Kth Smallest Element in a BST
    229. Majority Element II
    228. Summary Ranges
    227. Basic Calculator II
    223. Rectangle Area
    222. Count Complete Tree Nodes
  • 原文地址:https://www.cnblogs.com/guaniu/p/2324030.html
Copyright © 2011-2022 走看看