jsp中可以直接插入java的程式碼,如下連結資料庫:<% String DBDRIVER="net.sourceforge.jtds.jdbc.Driver"; String DBURL="jdbc:jtds:sqlserver://127.0.0.1:1433/資料庫名"; String DBUSER="登入名"; String PASSWORD="登入密碼"; try{ Class.forName(DBDRIVER); Connection cn=DriverManager.getConnection(DBURL,DBUSER,PASSWORD); Statement st=cn.createStatement(); String sql="SELECT 欄位1,欄位2,欄位3,欄位4,欄位5 from 表名"; ResultSet rs=st.executeQuery(sql); while(rs.next()){ String sno=rs.getString("欄位1"); String sname=rs.getString("欄位2"); String sex=rs.getString("欄位3"); java.sql.Date birthday=rs.getDate("欄位四"); String sd=rs.getString("欄位5"); out.print(sno+","+sname+","+sex+","+birthday+","+sd+"<br>"); } rs.close();//關閉結果集 cn.close();//關閉操作 } catch(Exception ex){ System.out.println(ex.getMessage()); System.out.println("連線異常"); ex.printStackTrace(); } %>
jsp中可以直接插入java的程式碼,如下連結資料庫:<% String DBDRIVER="net.sourceforge.jtds.jdbc.Driver"; String DBURL="jdbc:jtds:sqlserver://127.0.0.1:1433/資料庫名"; String DBUSER="登入名"; String PASSWORD="登入密碼"; try{ Class.forName(DBDRIVER); Connection cn=DriverManager.getConnection(DBURL,DBUSER,PASSWORD); Statement st=cn.createStatement(); String sql="SELECT 欄位1,欄位2,欄位3,欄位4,欄位5 from 表名"; ResultSet rs=st.executeQuery(sql); while(rs.next()){ String sno=rs.getString("欄位1"); String sname=rs.getString("欄位2"); String sex=rs.getString("欄位3"); java.sql.Date birthday=rs.getDate("欄位四"); String sd=rs.getString("欄位5"); out.print(sno+","+sname+","+sex+","+birthday+","+sd+"<br>"); } rs.close();//關閉結果集 cn.close();//關閉操作 } catch(Exception ex){ System.out.println(ex.getMessage()); System.out.println("連線異常"); ex.printStackTrace(); } %>