<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.sql.*"%>
<html>
<head>
<title>连接mysql数据库 </title>
</head>
<body>
<%
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=123");
}catch(SQLException e){
e.printStackTrace();
}
if(conn!=null){
out.print("已经成功连接SQL Sever数据库!");}
conn.close();//这里提示错误
%>
</body>
</html>
我的mysql是绿色版的
为什么连不上?
还有为什么连接数据库时不需要 传数据库的地址?
import="java.sql.*"%>
<html>
<head>
<title>连接mysql数据库 </title>
</head>
<body>
<%
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=123");
}catch(SQLException e){
e.printStackTrace();
}
if(conn!=null){
out.print("已经成功连接SQL Sever数据库!");}
conn.close();//这里提示错误
%>
</body>
</html>
我的mysql是绿色版的
为什么连不上?
还有为什么连接数据库时不需要 传数据库的地址?
localhost 你本机
mydata 数据库名
root 用户名
123 密码
localhost就是数据库的IP地址,就是本机,也可以使用本机的IP地址貌似要在数据库主机后面加上端口号的改为这样试试conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata?user=root&password=123");
import="java.sql.*"%>
<html>
<head>
<title>连接mysql数据库 </title>
</head>
<body>
<%
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata?user=root&password=123");
}catch(SQLException e){
e.printStackTrace();
}
finally{
try{
if(!conn.isClosed())
{
out.print("已成功连接SQL数据库!");
conn.close();
}
else{
out.print("连接不上");
}
}catch(Exception ex){}
}
%>
</body>
</html>
改成这样`没有错误``但页面上什么东西都没有,也没输出连没连上
我的mysql是绿色版的``会有影响吗?