java.lang.NullPointerException错误的代码补充
连接数据库类:packagecom.wsy;importjava.sql.Connection;importjava.sql.DriverManager;importja...
连接数据库类:package com.wsy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class connsqlserver {
/**
* @param args
*/
private Connection cn = null;
public Connection getConnection() {
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
cn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_net","sa","");
System.out.println("连接成功");
// Statement smt=cn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
return cn;
}
public ResultSet executeQuery(String sql) {
if (cn == null)
getConnection();
try {
return cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
}
}
public int executeUpdate(String sql) {
if (cn == null)
getConnection();
try {
return cn.createStatement().executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
}
}
public void close() {
try {
cn.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
cn = null;
}
}
}
查看、添加、删除等功能处理类:package com.wsy;
import java.sql.*;
import java.util.*;
import java.text.*;
public class selectsql {
public static ResultSet rs;
public static StringTrans s;
public static connsqlserver connsqlserver=new connsqlserver();
/*
* 关于tb_usertable表的操作
*/
public static Collection getRet(){
Collection ret=new ArrayList();
try{
connsqlserver connsqlserver=new connsqlserver();
String sql="select * from tb_usertable";
rs=connsqlserver.executeQuery(sql);
while(rs.next()){
String names=rs.getString("name");
String passwords=rs.getString("password");
user user=new user();
user.setName(names);
user.setPassword(passwords);
ret.add(user);
}
}
catch(Exception e){
e.printStackTrace();
}
connsqlserver.close();
return ret;
}
public static int check(String name,String password){
int i=0;
String names="";
String passwords="";
try{
connsqlserver connsqlserver=new connsqlserver();
String sql="select * from tb_usertable where name='"+name+"'and password='"+password+"'";
System.out.println(sql);
rs=connsqlserver.executeQuery(sql);
while(rs.next()){
names=rs.getString("name");
passwords=rs.getString("password");
if(names!=null){
i=1;
}
}
}
catch(Exception e){
e.printStackTrace();
}
connsqlserver.close();
return i;
}
/*
* 关于tb_news表的操作
*/ 展开
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class connsqlserver {
/**
* @param args
*/
private Connection cn = null;
public Connection getConnection() {
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
cn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_net","sa","");
System.out.println("连接成功");
// Statement smt=cn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
return cn;
}
public ResultSet executeQuery(String sql) {
if (cn == null)
getConnection();
try {
return cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
}
}
public int executeUpdate(String sql) {
if (cn == null)
getConnection();
try {
return cn.createStatement().executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
}
}
public void close() {
try {
cn.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
cn = null;
}
}
}
查看、添加、删除等功能处理类:package com.wsy;
import java.sql.*;
import java.util.*;
import java.text.*;
public class selectsql {
public static ResultSet rs;
public static StringTrans s;
public static connsqlserver connsqlserver=new connsqlserver();
/*
* 关于tb_usertable表的操作
*/
public static Collection getRet(){
Collection ret=new ArrayList();
try{
connsqlserver connsqlserver=new connsqlserver();
String sql="select * from tb_usertable";
rs=connsqlserver.executeQuery(sql);
while(rs.next()){
String names=rs.getString("name");
String passwords=rs.getString("password");
user user=new user();
user.setName(names);
user.setPassword(passwords);
ret.add(user);
}
}
catch(Exception e){
e.printStackTrace();
}
connsqlserver.close();
return ret;
}
public static int check(String name,String password){
int i=0;
String names="";
String passwords="";
try{
connsqlserver connsqlserver=new connsqlserver();
String sql="select * from tb_usertable where name='"+name+"'and password='"+password+"'";
System.out.println(sql);
rs=connsqlserver.executeQuery(sql);
while(rs.next()){
names=rs.getString("name");
passwords=rs.getString("password");
if(names!=null){
i=1;
}
}
}
catch(Exception e){
e.printStackTrace();
}
connsqlserver.close();
return i;
}
/*
* 关于tb_news表的操作
*/ 展开
4个回答
展开全部
想问什么
追问
java.lang.NullPointerException
at com.wsy.connsqlserver.close(connsqlserver.java:57)
at com.wsy.selectsql.selectNewsforqyxw(selectsql.java:175)
at org.apache.jsp.qyxw_jsp._jspService(qyxw_jsp.java:94)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
异常信息都不给拿啥看?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询