Home > Java > javaTutorial > jsp connection mysql code reference ~ available for personal testing

jsp connection mysql code reference ~ available for personal testing

PHP中文网
Release: 2017-07-15 11:43:24
Original
1615 people have browsed it

For phpers who are learning phpprogramming, you may try to use jspstudy to build a jsp development environment,

But for you who don’t understand java, you want to connectmysql database is a very complicated matter.

The following is the feasible code that I tested and compiled. I hope it can help you who are confused.

The code is pasted:

<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
 //此进行连接数据库
String url="jdbc:mysql://127.0.0.1:3306/test"; //test为数据库名称
String dbuser="root"; //数据库账户
String dbpwd="root"; //数据库密码
try 
{
    Class.forName("com.mysql.jdbc.Driver"); //加载驱动 JspStudy
} 
catch (ClassNotFoundException e) 
{ 
    e.printStackTrace();
}  
//取得数据库连接conn JspStudy
Connection conn=DriverManager.getConnection(url, dbuser, dbpwd);;
PreparedStatement ps=null;
ResultSet rs=null; 
String username="";
String pwd="";
String name="";
try 
{ 
    String sql="select id,username,password,name from users";
    ps = conn.prepareStatement(sql);
    rs = ps.executeQuery();
    while(rs.next())
    {
        username=rs.getString(2);
        pwd=rs.getString(3);
        name=rs.getString(4);
        out.println("账户:"+username+"   密码:"+pwd+"   姓名:"+name+"<br>"); 
    }
}
catch (SQLException e) 
{
    e.printStackTrace();
}
finally
{ 
        try 
        {
            if(rs!=null)            
                rs.close();
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
        finally
        {
                try 
                {
                    if(ps!=null)                    
                        ps.close();
                } 
                catch (SQLException e) 
                { 
                    e.printStackTrace();
                }
                finally
                {
                    try 
                    {
                        if(conn!=null)                    
                            conn.close();
                    } 
                    catch (SQLException e) 
                    { 
                            e.printStackTrace();
                    }
                }
        }
} 
%>
Copy after login

This article is provided by php Chinese website. It tells an example of basic jsp connecting to mysql database. The code has been tested and compiled by myself and can be run.

Article address: //m.sbmmt.com/java-article-374268.html

Please do not reprint~~~~~

The above is the detailed content of jsp connection mysql code reference ~ available for personal testing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template