Y
youjq
Unregistered / Unconfirmed
GUEST, unregistred user!
父类代码如下:
package base;
import java.io.*;
import java.sql.*;
public class db {
Connection conn = null ;
ResultSet rs = null;
String Username="";
String Password="";
String Eamil="";
String Homepage="";
String Signs="";
public db() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e){
System.err.println("mydb():" + e.getMessage());
}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
conn = DriverManager.getConnection("jdbcdbc:MySQL","root","root");
Statement stmt = conn.createStatement();
//执行数据库查询操作
rs = stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("db.executeQuery:"+ex.getMessage());
}
return rs;
}
public boolean executeUpdate(String sql){
boolean bupdate = false;
rs = null;
try{
conn = DriverManager.getConnection("jdbcdbc:MySQL","root","root");
Statement stmt = conn.createStatement();
int rowCount = stmt.executeUpdate(sql);
if (rowCount!=0)bupdate=true;
}
catch(SQLException ex) {
System.err.println("db.executeUpdate:" + ex.getMessage());
}
return bupdate;
}
public static String toChinese(String strvalue) {
try{
if(strvalue==null)
{
return null;
}
else
{
strvalue = new
String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}
catch(Exception e)
{
return null;
}
}
}
子类代码如下:
package base;
import java.sql.*;
import java.lang.*;
public class adduser extends db {
public boolean addNewUser(){
boolean boadduser=false;
try{
String sSql = new String("insert into user (regtime,username,password,email,homepage,signs)");
sSql = sSql+" values (SYSDAYE,''+Username+'',''+Password+'',''+Email+'',''+Homepage+'',''+Signs+'')";
System.out.println(sSql);
if(super.executeUpdate(sSql))boadduser=true;
}
catch(Exception ex){
System.err.println("adduser.addNewUser:" + ex.getMessage());
}
finally{
return boadduser;
}
}
public boolean checkUser(){
boolean boadduser=false;
try{
String sSql = "select * from user where username=''+Username+''";
if((super.executeQuery(sSql)).next()){
boadduser=false;
}else
{
boadduser=true;
}
}
catch(Exception ex) {
System.err.println("adduser.addNewUser:= " + ex.getMessage());
}finally{
return boadduser;
}
}
public String getUsername(){
return Username;
}
public void setUsername(String newUsername){
Username = db.toChinese(newUsername);}
public String getPassword(){
return Password;
}
public void setPassword(String newPassword){
Password = newPassword;
}
public String getEmail(){
return Email;
}
public void setEmail(String newEmail){
Email = newEmail;
}
public String getHomepage(){
return Homepage
}
public void setHomepage(String newHomepage){
Homepage = newHomepage;
}
//属性Signs的get/set方法
public String getSigns(){
return Signs;
}
public void setSigns(String newSigns){
Signs = db.toChinese(newSigns);
}
为什么子类中不认识父类中的所有变量和方法如Username,Password等。
难道public class adduser extends db {
不是这样调用的吗?
package base;
import java.io.*;
import java.sql.*;
public class db {
Connection conn = null ;
ResultSet rs = null;
String Username="";
String Password="";
String Eamil="";
String Homepage="";
String Signs="";
public db() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e){
System.err.println("mydb():" + e.getMessage());
}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
conn = DriverManager.getConnection("jdbcdbc:MySQL","root","root");
Statement stmt = conn.createStatement();
//执行数据库查询操作
rs = stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("db.executeQuery:"+ex.getMessage());
}
return rs;
}
public boolean executeUpdate(String sql){
boolean bupdate = false;
rs = null;
try{
conn = DriverManager.getConnection("jdbcdbc:MySQL","root","root");
Statement stmt = conn.createStatement();
int rowCount = stmt.executeUpdate(sql);
if (rowCount!=0)bupdate=true;
}
catch(SQLException ex) {
System.err.println("db.executeUpdate:" + ex.getMessage());
}
return bupdate;
}
public static String toChinese(String strvalue) {
try{
if(strvalue==null)
{
return null;
}
else
{
strvalue = new
String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}
catch(Exception e)
{
return null;
}
}
}
子类代码如下:
package base;
import java.sql.*;
import java.lang.*;
public class adduser extends db {
public boolean addNewUser(){
boolean boadduser=false;
try{
String sSql = new String("insert into user (regtime,username,password,email,homepage,signs)");
sSql = sSql+" values (SYSDAYE,''+Username+'',''+Password+'',''+Email+'',''+Homepage+'',''+Signs+'')";
System.out.println(sSql);
if(super.executeUpdate(sSql))boadduser=true;
}
catch(Exception ex){
System.err.println("adduser.addNewUser:" + ex.getMessage());
}
finally{
return boadduser;
}
}
public boolean checkUser(){
boolean boadduser=false;
try{
String sSql = "select * from user where username=''+Username+''";
if((super.executeQuery(sSql)).next()){
boadduser=false;
}else
{
boadduser=true;
}
}
catch(Exception ex) {
System.err.println("adduser.addNewUser:= " + ex.getMessage());
}finally{
return boadduser;
}
}
public String getUsername(){
return Username;
}
public void setUsername(String newUsername){
Username = db.toChinese(newUsername);}
public String getPassword(){
return Password;
}
public void setPassword(String newPassword){
Password = newPassword;
}
public String getEmail(){
return Email;
}
public void setEmail(String newEmail){
Email = newEmail;
}
public String getHomepage(){
return Homepage
}
public void setHomepage(String newHomepage){
Homepage = newHomepage;
}
//属性Signs的get/set方法
public String getSigns(){
return Signs;
}
public void setSigns(String newSigns){
Signs = db.toChinese(newSigns);
}
为什么子类中不认识父类中的所有变量和方法如Username,Password等。
难道public class adduser extends db {
不是这样调用的吗?