对于学习php编程的phper来说,可能你会尝试使用jspstudy搭建个jsp开发环境,
但是对于不懂java的你来说,想连接mysql数据库都是个很复杂的事情,
下面是本人测试整理的可行代码,希望能帮助到迷茫中的你,
代码贴上:
<%@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();
}
}
}
}
%>本文由php中文网提供,讲述基本的jsp连接mysql数据库的实例,代码经过本人的测试整理,可以运行。
文章地址:http://www.php.cn/java-article-374268.html
请勿转载~~~~~
以上就是jsp连接mysql代码参考~亲测可用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号