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+"
");
}
rs.close();//关闭结果集
cn.close();//关闭操作
}
catch(Exception ex){
System.out.println(ex.getMessage());
System.out.println("连接异常");
ex.printStackTrace();
}
%>