无法从静态上下文中引用非静态 变量错误

tjficcbw 2010-04-20 07:50:22
我做了个数据库的操作类,总是调不好,谢谢各位,帮个忙给调试一下,代码如下。


import java.sql.*;

public class SqlHelper {



public Connection con = null;
public ResultSet result = null;

public void SelectException (String SelectSqlString) {


try{
// 为这个连接建立SQL语句容器
Statement select = con.createStatement();
// Statement select = con.prepareStatement( );//用于复杂的SQL语句

// 执行SQL语句查询获得字符集
ResultSet result = select.executeQuery(SelectSqlString);
} catch (Exception e) {
System.out.println(SelectSqlString);// 看SQL语句查过来没
e.printStackTrace();
}
}


public void CloseConn(){
if (con != null) {
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}



public void SetConn() {
// 定义数据库地址
String url = "jdbc:mysql://localhost:3306/ccentertainment";
// 定义数据库连接
//Connection con = null;
try {
// 定义数据库驱动
String driver = "com.mysql.jdbc.Driver";
// 加载数据库驱动
Class.forName(driver).newInstance();
} catch (Exception e) {
System.out.println("加载数据库驱动失败!");
return;
}
try {
// 连接数据库
con = DriverManager.getConnection(url, "root", "root");
}catch (Exception e) {
e.printStackTrace();
}
}
}
#########################################################

import java.sql.*;

public class ShowDbTest {

ResultSet Sresult = null;

public static void main(String args[]) {
SqlHelper ShValue = new SqlHelper();
ShValue.SetConn();
ShValue.SelectException("select * from user");
Sresult = ShValue.result;
try {
while (Sresult.next()) { // process results one row at a time
System.out.println(Sresult.getInt(1) + Sresult.getInt(2));
}
}

catch (Exception e) {
e.printStackTrace();
}
ShValue.CloseConn();
}
}

#############################################

---------- 编译 ----------
ShowDbTest.java:11: 无法从静态上下文中引用非静态 变量 Sresult
Sresult = ShValue.result;
^
ShowDbTest.java:13: 无法从静态上下文中引用非静态 变量 Sresult
while (Sresult.next()) { // process results one row at a time
^
ShowDbTest.java:14: 无法从静态上下文中引用非静态 变量 Sresult
System.out.println(Sresult.getInt(1) + Sresult.getInt(2));
^
ShowDbTest.java:14: 无法从静态上下文中引用非静态 变量 Sresult
System.out.println(Sresult.getInt(1) + Sresult.getInt(2));
^
4 错误

输出完成 (耗时 1 秒)

...全文
397 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tjficcbw 2010-04-20
  • 打赏
  • 举报
回复
谢谢,成了,我刚学几天,justlearn回复的,我看不太懂但每次全说中问题的主要方面
Mars_Ma_OK回复的我才能参考,惭愧呀,谢谢了,
justlearn 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tjficcbw 的回复:]
3楼编译通过但,不显示内容,不知为什么。
[/Quote]
// 执行SQL语句查询获得字符集
ResultSet result = select.executeQuery(SelectSqlString);
这里重复定义了,你在类成员变量里定义了一个,这里重新又定义了,所以后来取到的值是null值
可以改成这样
result = select.executeQuery(SelectSqlString);
Mars_Ma_OK 2010-04-20
  • 打赏
  • 举报
回复

public Connection con = null;
public ResultSet result = null;

public void SelectException (String SelectSqlString) {


try{
// 为这个连接建立SQL语句容器
Statement select = con.createStatement();
// Statement select = con.prepareStatement( );//用于复杂的SQL语句

// 执行SQL语句查询获得字符集
result = select.executeQuery(SelectSqlString); //这样才正确!!!!
} catch (Exception e) {
System.out.println(SelectSqlString);// 看SQL语句查过来没
e.printStackTrace();
}
}

Mars_Ma_OK 2010-04-20
  • 打赏
  • 举报
回复
2的补充..因为,你调用的是SqlHelper 这个类的属性.但是,你有没有往这个属性设值?



public Connection con = null;
public ResultSet result = null;

public void SelectException (String SelectSqlString) {


try{
// 为这个连接建立SQL语句容器
Statement select = con.createStatement();
// Statement select = con.prepareStatement( );//用于复杂的SQL语句

// 执行SQL语句查询获得字符集
ResultSet result = select.executeQuery(SelectSqlString);//这不是你外面设置的属性,因为,此处为局部变量.你没有给全局变量设值!!! } catch (Exception e) {
System.out.println(SelectSqlString);// 看SQL语句查过来没
e.printStackTrace();
}
}

Mars_Ma_OK 2010-04-20
  • 打赏
  • 举报
回复
楼主有两个错误:
1:ResultSet Sresult = null; //你声明的不是static所以,你在static main()方法里面调用,肯定是出错的.


import java.sql.*;

public class ShowDbTest {

//ResultSet Sresult = null; //此处,你声明的不是static所以,你在static main()方法里面调用,肯定是出错的.

public static void main(String args[]) {
ResultSet Sresult = null
SqlHelper ShValue = new SqlHelper();
ShValue.SetConn();
ShValue.SelectException("select * from user");
Sresult = ShValue.result;
try {
while (Sresult.next()) { // process results one row at a time
System.out.println(Sresult.getInt(1) + Sresult.getInt(2));
}
}

catch (Exception e) {
e.printStackTrace();
}
ShValue.CloseConn();
}
}



2. NullPointException: Sresult = ShValue.result; //此处Sresult 肯定为null.
你在代码

Sresult = ShValue.result;
try {
while (Sresult.next()) { // process results one row at a time
// null.next(); 肯定出错!!!
System.out.println(Sresult.getInt(1) + Sresult.getInt(2));
}
}


tjficcbw 2010-04-20
  • 打赏
  • 举报
回复
没字符集
tjficcbw 2010-04-20
  • 打赏
  • 举报
回复


import java.sql.*;

public class ShowDbTest {


public static void main(String args[]) {
//把定义换到方法体内,静态方法不能引用非静态成员
ResultSet Sresult = null;
SqlHelper ShValue = new SqlHelper();
ShValue.SetConn();
ShValue.SelectException("select * from user");
Sresult = ShValue.result;
try {
//最好加上null判断
System.out.println("路过");
if(Sresult!=null){
while (Sresult.next()) { // process results one row at a time
System.out.println(Sresult.getInt(1) + Sresult.getInt(2));
System.out.println("计算");
}
}else{
System.out.println("没字符集");

}
}

catch (Exception e) {
e.printStackTrace();
}
ShValue.CloseConn();
}
}

tjficcbw 2010-04-20
  • 打赏
  • 举报
回复
3楼编译通过但,不显示内容,不知为什么。
coolbamboo2008 2010-04-20
  • 打赏
  • 举报
回复
在main方法里面,先new一个ShowDbTest对象
Ark032425 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 justlearn 的回复:]
改了2点,看注释

Java code

import java.sql.*;

public class ShowDbTest {


public static void main(String args[]) {
//把定义换到方法体内,静态方法不能引用非静态成员
ResultSet Sresult = null;

……
[/Quote]
在外面要定义成static变量才能直接用;否则要new 一个对象
justlearn 2010-04-20
  • 打赏
  • 举报
回复
改了2点,看注释

import java.sql.*;

public class ShowDbTest {


public static void main(String args[]) {
//把定义换到方法体内,静态方法不能引用非静态成员
ResultSet Sresult = null;

SqlHelper ShValue = new SqlHelper();
ShValue.SetConn();
ShValue.SelectException("select * from user");
Sresult = ShValue.result;
try {
//最好加上null判断
if(Sresult!=null){
while (Sresult.next()) { // process results one row at a time
System.out.println(Sresult.getInt(1) + Sresult.getInt(2));
}
}
}

catch (Exception e) {
e.printStackTrace();
}
ShValue.CloseConn();
}
}

canoe982 2010-04-20
  • 打赏
  • 举报
回复
LZ应该去了解下类的静态属性与一般属性的区别。
北狐狸 2010-04-20
  • 打赏
  • 举报
回复
#########################################################

import java.sql.*;

public class ShowDbTest {

//ResultSet Sresult = null;//或者在此行加static注明

public static void main(String args[]) {
ResultSet Sresult = null;//在此行声明
SqlHelper ShValue = new SqlHelper();
ShValue.SetConn();
ShValue.SelectException("select * from user");
Sresult = ShValue.result;
try {
while (Sresult.next()) { // process results one row at a time
System.out.println(Sresult.getInt(1) + Sresult.getInt(2));
}
}

catch (Exception e) {
e.printStackTrace();
}
ShValue.CloseConn();
}
}

#############################################

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧