jsp连接数据库代码

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
   
   
    <%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%

 

 

String userName="root";
String userPassword="678663";

String url="jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=gb2312";
  

String errorMessage="";

 
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();

 

Connection conn=DriverManager.getConnection(url,userName,userPassword);

if(conn!=null)
{
out.print("yes!"); 
}
else
{
out.print("wrong!"); 
}


}catch(Exception e)
{
 
 errorMessage=e.toString();
 out.print(errorMessage);
 
}

 

%>

 

</body>
</html>

posted @ 2011-10-06 22:08  lengye__2011  阅读(410)  评论(0)    收藏  举报