各種的jsp資料庫連接方法代碼!(以前收集的)

來源:互聯網
上載者:User


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>

 

如上的串連方法只能作為學習之用!和簡單個人首頁

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.