1.與access <%@page contentType = "text/html;charset=GB2312" import = "java.sql
.*"%>
<%@page import = "java.util.*,java.text.*"%>
<html>
<title>取出並顯示表中的資訊</title>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:webdata");
Statement smt = con.createStatement();
String sql;
sql = "select bh,shrxx,shrdh,shrdz,hwjs,bzxs,yf,bjd from hy";
ResultSet rs = smt.executeQuery(sql);
out.println("取出表中的的所有資訊");
%>
請點擊這裡</font><a href="javascript:history.back(1)"><b><font color="#FF0000"
>返回</font></b></a><hr>
<%
out.println("<table border = '1'>");
out.println("<tr bgcolor = 'yellow'><th>貨單號</th><th>收貨人姓名</th><th>收貨
人電話</th><th>收貨人住址</th><th>貨件數量</th><th>封裝形式</th><th>運費</th><
th>報價單</th></tr>");
while(rs.next())
out.println("<tr><td>" + rs.getString(1) + "</td><td>" + rs.getString(2) + "</
td><td>" + rs.getString(3) + "</td><td>" + rs.getString(4) + "</td><td>" + rs.
getString(5) + "</td><td>" + rs.getString(6) + "</td><td>" + rs.getString(7) +
"</td><td>" + rs.getString(8) + "</td></tr>" );
con.close();
%>
<html>
<body>
</table>
</body>
</html>
2.與mysql
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://192.168.0.1:3306/test?user=root&password=123";
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.
CONCUR_UPDATABLE);
String sql="select * from huahe_article";
ResultSet rs=stmt.executeQuery(sql); %>
<table><tr><td>您的第一個欄位內容為:</td>
<td>您的第二個欄位內容為:</td></tr>
<%
while(rs.next()) {%>
<tr><td>
<%=rs.getString(1)%> </td><td>
<%=rs.getString(2)%> </td></tr>
<%}%>
</table>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
3.與mssql
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%!
public static String getGBString(String src) {
try {
return new String(src.getBytes("ISO-8859-1"), "gb2312");
} catch (java.io.UnsupportedEncodingException e) {
return null;
}
}
%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://192.168.0.1:1433;DatabaseName=test";
//test你的資料庫的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.
CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=getGBString(rs.getString(1))%> <br>
您的第二個欄位內容為:<%=getGBString(rs.getString(2))%> <br>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%> <br>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
如上的串連方法只能作為學習之用!和簡單個人首頁