3个回答
2013-09-18
展开全部
<%@ page language="java" import="java.util.*" import="java.sql.*"
contentType="text/html;charset=gb2312"%><%@page import="com.mysql.jdbc.Connection"%>
<%@page import="java.sql.SQLException"%>
<html>
<head>
<title>连接MySql数据库</title>
</head> <body>
<%
String url="jdbc:mysql://localhost:3306/javaweb?user=root&password=cczly";
java.sql.Connection conn =null;
Statement stmt = null;
ResultSet rs =null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
}catch(ClassNotFoundException e){
out.println("找不到驱动类");
}catch(SQLException e){
out.print("连接数据库失败!");
}
try{
stmt = conn.createStatement();
String queryAll ="select * from user;";
rs = stmt.executeQuery(queryAll);
while(rs.next()){
int userid = rs.getInt(1);
String username = rs.getString(2);
String userpassword = rs.getString(3);
out.println("用户id:"+userid+",");
out.println("用户名:"+username+",");
out.println("用户密码:"+userpassword+"<br>");
}
}catch(SQLException e){
out.println("查询所有用户失败!");
}
try{
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
}catch(Exception e){
out.println("数据库关闭异常");
}
%>
</body>
</html>
contentType="text/html;charset=gb2312"%><%@page import="com.mysql.jdbc.Connection"%>
<%@page import="java.sql.SQLException"%>
<html>
<head>
<title>连接MySql数据库</title>
</head> <body>
<%
String url="jdbc:mysql://localhost:3306/javaweb?user=root&password=cczly";
java.sql.Connection conn =null;
Statement stmt = null;
ResultSet rs =null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
}catch(ClassNotFoundException e){
out.println("找不到驱动类");
}catch(SQLException e){
out.print("连接数据库失败!");
}
try{
stmt = conn.createStatement();
String queryAll ="select * from user;";
rs = stmt.executeQuery(queryAll);
while(rs.next()){
int userid = rs.getInt(1);
String username = rs.getString(2);
String userpassword = rs.getString(3);
out.println("用户id:"+userid+",");
out.println("用户名:"+username+",");
out.println("用户密码:"+userpassword+"<br>");
}
}catch(SQLException e){
out.println("查询所有用户失败!");
}
try{
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
}catch(Exception e){
out.println("数据库关闭异常");
}
%>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
七鑫易维信息技术
2024-09-02 广告
2024-09-02 广告
Play Video 七鑫易维是致力于机器视觉和人工智能领域的高新科技企业,迄今已专注眼球追踪技术的研发、创新与应用超过14年,拥有完全自主知识产权,全球专利总量655余项。 作为眼球追踪技术领域的全球知名品牌,七鑫易维的产品体系覆盖眼动分...
点击进入详情页
本回答由七鑫易维信息技术提供
2013-09-18
展开全部
<script language="javascript"> //用 javascript 写服务器端连接数据库的代码示例 var conn = new ActiveXObject("ADODB.Connection"); conn.Open("Provider=SQLOLEDB.1; Data Source=localhost; User ID=sa; " +"Password=; Initial Catalog=pubs"); var rs = new ActiveXObject("ADODB.Recordset"); var sql="select * from authors"; rs.open(sql, conn); shtml = "<table width='100%' border=1>"; shtml +="<tr bgcolor='#f4f4f4'><td>au_id</td><td>au_lname</td><td>au_fname</td><td>phone</td><td>address</td><td> city</td><td>state</td><td>zip</td></tr>"; while(!rs.EOF) { shtml += "<tr><td>" + rs("au_id") + "</td><td>" + rs("au_lname") + "</td><td>" + rs("au_fname") + "</td><td>" + rs("phone") + "</td><td>" + rs("address") + "</td><td>" + rs("city") + "</td><td>" + rs("state") + "</td><td>" + rs("zip") + "</td></tr>"; rs.moveNext; } shtml += "</table>"; document.write(shtml); rs.close(); rs = null; conn.close(); conn = null; </script>
本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/xwdengjie/archive/2008/06/17/2555955.aspx
本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/xwdengjie/archive/2008/06/17/2555955.aspx
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-18
展开全部
用JavaScript连接数据库- -!GOD......JavaScript是客户端脚本,和服务机毛关系都没有,怎么可能去连接数据库……要连接也要用Ajax或者JQuery之类的技术...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询