100分求.......jsp方法为什么连接不了数据库??的解决方法
<%!
private void myfun(int Class1){
String sql="select * from User_News_Site where News_Class="+Class1+"";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
if (rs.getString("News_Name")!=null) {
System.out.println(rs.getString("News_Name"));
}
else
{
System.out.println("没有此类新闻");
}
}
}
%>
补充:已经包含了数据库连接文件 <%@ includ file=include/sql_class.jsp%>
错误是:An error occurred at line: 10 in the jsp file: /2myhome/rzbt/ph.jsp
Generated servlet error:
stmt
cannot be resolved------解决方案--------------------
把
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://121.56.35.243:1433;DatabaseName=jsp_data";
//pubs为你的数据库的
String user="sa";
String password="5a391e15";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
request.setCharacterEncoding("gb2312");//设置语言
放到
String sql="select * from User_News_Site where News_Class="+Class1+"";
上面....
------解决方案--------------------
private void myfun(int Class1){}中不能有request
请把
request.setCharacterEncoding("gb2312");//设置语言
放在方法及类的外面
如有需要则需要方法中加入参数
即 private void myfun(int Class1,HTTPServletRequest request){}
------解决方案--------------------
<%Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
.newInstance();
String url="jdbc:microsoft:sqlserver://121.56.35.243:1433;DatabaseName=jsp_data";
//pubs为你的数据库的
String user="sa";
String password="5a391e15";
conn = DriverManager.getConnection(url, user,
password);
System.out.println(conn);
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
request.setCharacterEncoding("gb2312");//设置语言
String sql="select * from User_News_Site where News_Class="+Class1+"";
rs = stmt.executeQuery(sql);
while (rs.next()) {
if (rs.getString("News_Name") != null) {
System.out.println(rs.getString("News_Name"));
} else {
System.out.println("没有此类新闻");
}
}
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (
SQLException e) {
e.printStackTrace();
}
}
%>