当前位置: 技术问答>java相关
一个数据类,有其他的类进行修改,
来源: 互联网 发布时间:2015-01-07
本文导语: 在测试时,向类statedb设置数据时是对的,但是取的时候不能成功,特向大侠们求救;以下为原程序代码; package onJava; import java.awt.*; import java.net.*; import java.sql.*; import java.io.*; import java.util.*; public class MakeDB{ ...
在测试时,向类statedb设置数据时是对的,但是取的时候不能成功,特向大侠们求救;以下为原程序代码;
package onJava;
import java.awt.*;
import java.net.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class MakeDB{
String workerString;
stateDB state = new stateDB();
public static void main(String[] args){
JFrame frame = new MakeDB();
}
public MakeDB() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:stockDB File";
String user = "";
String password = "";
Connection con = DriverManager.getConnection(url,user,password);
Statement stmt = con.createStatement();
System.out.println(stmt);
// 此句设置完全成功
state.setStmt(stmt);
state.setCon(con);
new seekDB();
}catch(Exception e){
System.out.println(e);
}
for(;;){};
}
}
class seekDB extends stateDB{ ///去掉 extends statedb也不行
Connection con;
//stateDB stmt = new stateDB(); ////去掉此句也不行
public seekDB(){
try{
Statement stmt = getStmt(); //去掉此句也不行,此时stmt为null
System.out.println(stmt);
String sqlStrings = "SELECT * FROM stockDay"; //----------
ResultSet rs = stmt.executeQuery(sqlStrings);
while(rs.next()) {
String workerString = rs.getString(2);
System.out.println(workerString);
}
}catch(Exception ex){
System.out.println(ex);
}
}
}
class stateDB{
Connection con;
Statement stmt;
public stateDB(){
}
public void setCon(Connection con){
con = con;
}
public Connection getCon(){
return con;
}
public void setStmt(Statement stmt){
stmt = stmt;
System.out.println(stmt); //测试用
}
public Statement getStmt(){
System.out.println(stmt); //测试用
return stmt;
}
}
package onJava;
import java.awt.*;
import java.net.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class MakeDB{
String workerString;
stateDB state = new stateDB();
public static void main(String[] args){
JFrame frame = new MakeDB();
}
public MakeDB() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:stockDB File";
String user = "";
String password = "";
Connection con = DriverManager.getConnection(url,user,password);
Statement stmt = con.createStatement();
System.out.println(stmt);
// 此句设置完全成功
state.setStmt(stmt);
state.setCon(con);
new seekDB();
}catch(Exception e){
System.out.println(e);
}
for(;;){};
}
}
class seekDB extends stateDB{ ///去掉 extends statedb也不行
Connection con;
//stateDB stmt = new stateDB(); ////去掉此句也不行
public seekDB(){
try{
Statement stmt = getStmt(); //去掉此句也不行,此时stmt为null
System.out.println(stmt);
String sqlStrings = "SELECT * FROM stockDay"; //----------
ResultSet rs = stmt.executeQuery(sqlStrings);
while(rs.next()) {
String workerString = rs.getString(2);
System.out.println(workerString);
}
}catch(Exception ex){
System.out.println(ex);
}
}
}
class stateDB{
Connection con;
Statement stmt;
public stateDB(){
}
public void setCon(Connection con){
con = con;
}
public Connection getCon(){
return con;
}
public void setStmt(Statement stmt){
stmt = stmt;
System.out.println(stmt); //测试用
}
public Statement getStmt(){
System.out.println(stmt); //测试用
return stmt;
}
}
|
笨笨帮你看一下。
|
caoco喜欢自言自语?