当前位置:Gxlcms > 数据库问题 > JavaWeb之原生数据库连接

JavaWeb之原生数据库连接

时间:2021-07-01 10:21:17 帮助过:11人阅读

//也可以采用ResourceBundle来解析与读取属性文件里的配置信息;我们在此采用Properties类加载属性文件//        ResourceBundle bundle = ResourceBundle.getBundle("db-config.properties");//        String DB_DRIVER=bundle.getString("db.DRIVER");//      ...        Properties properties=new Properties();        InputStream resourceAsStream = Util_1_JDBC.class.getClassLoader().getResourceAsStream("db-config.properties");        try {            properties.load(resourceAsStream);            String DB_DRIVER = properties.getProperty("db.DRIVER");            String DB_URL = properties.getProperty("db.URL");            String DB_USER = properties.getProperty("db.USERNAME");            String DB_PASSWORD = properties.getProperty("db.PASSWROD");        } catch (IOException e) {            throw new RuntimeException("读取属性文件失败!");        }        try {            Class.forName(DB_DRIVER);        } catch (ClassNotFoundException e) {            throw new RuntimeException("注册驱动失败!");        }    }    /**     * 打开与数据库的连接     * @return 一个连接对象     */    public static Connection openConnection(){        try {            return DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);        } catch (SQLException e) {            e.printStackTrace();        }        return null;    }    //释放资源    public static void release(Connection connection, Statement statement, ResultSet resultSet){        if(connection!=null){            try {                connection.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if (statement != null) {            try {                statement.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if (resultSet != null) {            try {                resultSet.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }}

 我们再来看LoginServlet.java的内容: 

import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;@WebServlet(name = "LoginServlet")public class LoginServlet extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");        String username = request.getParameter("username");        System.out.println("username="+username);        String userpwd = request.getParameter("userpwd");        System.out.println("userpwd="+userpwd);        PreparedStatement statement=null;        ResultSet resultSet=null;        //1.不采用数据源连接池,需要频繁建立与数据库的连接与释放资源 Connection connection = UtilJDBC.openConnection();        try {        statement=connection.prepareStatement("SELECT * FROM users where username=? AND  userpwd=?");        statement.setString(1,username);        statement.setString(2,userpwd);        resultSet = statement.executeQuery();        if(resultSet.next()){            System.out.println("登录成功!");        }else{            System.out.println("登录失败!");        }    } catch (SQLException e) {        e.printStackTrace();    }finally {        Util_1_JDBC.release(connection,statement,resultSet);    }

      }  

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      doPost(request, response);    }}

 另外,数据库名称为itszt2,要访问的表users的结构为:

CREATE TABLE `users` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `username` varchar(20) NOT NULL,  `userpwd` varchar(20) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

该表中存了一条数据,即(“xiaoming”,123456)。

读者可以在自己的电脑上对上述代码进行测试,以观察数据库连接,以及练习sql语句的若干操作。   

JavaWeb之原生数据库连接

标签:mysq   connect   用户名   where   throw   mapping   prot   连接池   out   

人气教程排行