值得收藏的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>?
?
?
?轉載于:https://www.cnblogs.com/ltb6w/p/9879984.html
總結
以上是生活随笔為你收集整理的值得收藏的JSP连接mysql数据库的例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS7中NAT网卡设置静态IP
- 下一篇: Linux学习笔记(3)linux服务管