• 值得收藏的JSP连接mysql数据库的例子


    1:用mysql驱动把mysql与tomcat的连接起来。把mysql驱动包(不用解压)放到Tomcat安装目录中lib文件夹下即可。

     2:然后在自己的新建的web应用程序上面就可以下下面的代码

    3:JDBC连接mysql数据库三步走

    第一首先加载数据库驱动,注册到驱动管理器Class.forName("com.mysql.jdbc.Driver");

    第二构建数据库连接URL,String URL="jdbc:mysql://localhost:3306/dudu";//dudu为自己创建的数据库,url格式:"jdbc协议:ip地址或者域名+端口+数据库名称"

    第三获取Connection对象 Connection conn=DriverManager.getConnection("root","123456",URL);//root为自己mysql的用户名,123456为自己mysql的密码

    解释说明:

    String url="jdbc:mysql://localhost:3306/dudu";//dudu为自己创建的数据库
    String username="root";//自己的mysql用户
    String password="123456";//自己的mysql的密码

    <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
    <%@page import="java.sql.DriverManager"%>
    <%@page import="java.sql.Connection"%>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Jsp链接数据库MySql</title>
    </head>
    
    <body>
    
      <% 
         
            try{
                
                Class.forName("com.mysql.jdbc.Driver");//记载数据库驱动,注册到驱动管理器
                String url="jdbc:mysql://localhost:3306/dudu";
                String username="root";
                String password="123456";
                Connection conn=DriverManager.getConnection(url,username,password);
                
            if(conn!=null){
                out.println("<h1>数据库连接成功!!!</h1>");
             }else{
                 out.println("数据库连接失败!!!"); 
            }
         }catch(ClassNotFoundException e){
            e.printStackTrace();
         }
           
      %>
      
    </body>
    </html>
     
  • 相关阅读:
    为用户添加角色
    WCF、MongoDB
    文件分布式存储实现例程
    Redis的Replication(复制)
    Lucene热词显示并选择
    Lucene热词统计
    通过队列解决Lucene文件并发创建索引
    Lucene.net应用
    Lucene.net
    Redis
  • 原文地址:https://www.cnblogs.com/ltb6w/p/9879984.html
Copyright © 2020-2023  润新知