测试javaweb jsp连接mysql数据库是否成功实例
自用测试
没有用到数据库的建表,但是必须首先建个数据库
小编的资源站www.miaozan.art
<%
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();
}
%>
项目程序一般加上下面代码,(数据库名“?”后面的)就是防止出现其他意想不到的错误
1. datasource.url=jdbc:mysql://localhost:3306/miao?
2 useUnicode=true&characterEncoding=utf-
3 8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC