返回站点首页
支持中心首页
常见问题搜索
常见问题
域名相关问题
空间相关问题
邮箱相关问题
智能建站相关问题
繁简通相关问题
中文域名相关问题
代理申请相关问题
 

   

JSP 连数据库(MySQL & SQLServer2000)

<%@page contentType="text/html;charset=utf-8"%>
<%
//声名
java.sql.Connection sqlConn;
java.sql.Statement sqlStmt;
java.sql.ResultSet sqlRst;
//regiester jdbc driver

String DBUser="d12345";
String DBPassword="12345678";
String DBServer="s509.now.net.cn"; // Can't use localhost , you must use IP or CNAME
String DBNAME="db_abc_com"; // change to your db name

Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//connect to the database
sqlConn=java.sql.DriverManager.getConnection
("jdbc:mysql://"+DBServer+":3306/"+DBNAME+"?useUnicode=true&characterEncoding=gb2312", DBUser,DBPassword);
//create the driver handle
sqlStmt=sqlConn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//excute sql
//sqlRst=sqlStmt.executeQuery("select * from test");
%>
<html>
<head>
<title>Linux-JSP-JDBC-MYSQL TESTING -SELETCT copyright <a href="mailto: sanry@newsea.net "> sanry@newsea.net </a> </title>
</head>
<body>
<table border="1" cellspace ="0" align="center">
<tr><td>DB CONNECT OK , </td></tr>
<tr>
<th>title </th>
<th>type </th>
</tr>
<%// while(sqlRst.next()) {%>
<tr>
<td> <%//=sqlRst.getString(1)%></td>
<td><%//=sqlRst.getString(2)%></td>
</tr>
<%//}%>
</table>
</body>
</html>
<%
//sqlRst.close();
sqlStmt.close();
sqlConn.close();
%>

 

下面是 SQL SERVER 2000的连法

<%@page contentType="text/html;charset=utf-8"%>
<%
//声名
java.sql.Connection sqlConn;
java.sql.Statement sqlStmt;
java.sql.ResultSet sqlRst;
//regiester jdbc driver
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//connect to the database
sqlConn=java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;DatabaseName=pubs;SelectMethod=direct","dxxx","");
//create the driver handle
sqlStmt=sqlConn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//excute sql
sqlRst=sqlStmt.executeQuery("select title_id,title,type from titles");
%>
<html>
<head>
<title>Linux-JSP-JDBC-MYSQL TESTING -SELETCT copyright sanry@newsea.net </title>
</head>
<body>
<table border="1" cellspace ="0" align="center">
<tr>
<th>title </th>
<th>type </th>
</tr>
<% while(sqlRst.next()) {%>
<tr>
<td> <%=sqlRst.getString(1)%></td>
<td><%=sqlRst.getString(2)%></td>
</tr>
<%}%>
</table>
</body>
</html>
<%
sqlRst.close();
sqlStmt.close();
sqlConn.close();
%>