您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JSP程序使用JDBC连接MySQL的教程

2024/3/16 19:42:20发布43次查看
安装和加载jdbc驱动程序
将里面的文件mysql-connector-java-5.1.7-bin.jar放在项目web-inf目录下的lib文件中,安装就已经完成了(前提是你的机器已经安装了mysql,如果没有安装先安装)
加载在jdbc驱动程序
<%@page language="java" contenttype="text/html;charset=gb2312"%> <!doctype html> <html> <head> <title>加载jdbc驱动程序</title> </head> <body> <% try{ class.forname("com.mysql.jdbc.driver");//加载jdbc驱动程序 }catch(classnotfoundexception e){ out.println("找不到驱动类");//抛出异常时,提示信息 } %> </body> </html>
连接mysql数据库
启动mysql和tomcat,
使用jdbc连接数据库。
第一种方式
<%@page language="java" contenttype="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!doctype html> <html> <head> <title>链接mysql数据库</title> </head> <body> <% try{ class.forname("com.mysql.jdbc.driver");//加载jdbc驱动程序 connection conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/javaweb?user=root&password=zhangda890126;;");//链接数据库 }catch(classnotfoundexception e){ out.println("找不到驱动类");//抛出异常时,提示信息 }catch(sqlexception e){ out.println("链接mysql数据库失败");//处理sqlexception异常 } %> </body> </html>
第二种方式
<%@page language="java" contenttype="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!doctype html> <html> <head> <title>链接mysql数据库</title> </head> <body> <% string url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址 string user = "root";//登录数据库的用户名 string password = "zhangda890126;;";//登录数据库的用户名的密码 try{ class.forname("com.mysql.jdbc.driver");//加载jdbc驱动程序 connection conn = drivermanager.getconnection(url,user,password);//链接数据库 }catch(classnotfoundexception e){ out.println("找不到驱动类");//抛出异常时,提示信息 }catch(sqlexception e){ out.println("链接mysql数据库失败");//处理sqlexception异常 } %> </body> </html>
更多jsp程序使用jdbc连接mysql的教程。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product