jsp 连接数据库的困惑!
请问:jsp连接数据库下面的代码怎么不行啊?先谢谢啦,刚开始学!是不是还有其他配置啊!
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<html>
<body>
<%Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs ";
//pubs为你的数据库的
String user= "sa ";
String password= "sa ";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql= "select * from authors ";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为: <%=rs.getString(1)%>
您的第二个字段内容为: <%=rs.getString(2)%>
<%}%>
<%out.print( "数据库操作成功,恭喜你 ");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
------解决方案-------------------- <%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<html>
<body>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();
String url= "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs ";
//pubs为你的数据库的
String user= "cwcs2 ";
String password= "cwcs2 ";
conn= DriverManager.getConnection(url,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql= "select * from authors ";
rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为: <%=rs.getString(1)%>
您的第二个字段内容为: <%=rs.getString(2)%>
<%}%>
<%out.print( "数据库操作成功,恭喜你 ");%>
<%rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
stmt.close();
conn.close();
e.printStackTrace();
}
%>
</body>
</html>
---------------------------------
覆盖你原来看看还有没有问题
你的JSP格式书写有点问题,还有没捕捉异常
------解决方案--------------------String user= "cwcs2 ";
String password= "cwcs2 ";
-----------------------------
用户名和密码忘记帮你改回来的,默认是都是sa
------解决方案-------------------- <%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<html>
<body>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
String url= "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=hzivr ";
//pubs为你的数据库的
String user= "sa ";
String password= "yw888 ";
conn= DriverManager.getConnection(url,user,password);