当前位置: 技术问答>java相关
小弟想学bean但是不知怎么开始
来源: 互联网 发布时间:2017-04-20
本文导语: 请各位能不能给一个简单的例子让我看一下。 | 给你一个我自已写的bean,给我email | package com.file; import java.io.*; public class WriteFile { private File myfile=null; private String WriteS...
请各位能不能给一个简单的例子让我看一下。
|
给你一个我自已写的bean,给我email
|
package com.file;
import java.io.*;
public class WriteFile
{
private File myfile=null;
private String WriteStr=null;
private String errstr="";
public WriteFile(String filepath,String WriteStr)
{
this.myfile=new File(filepath);
this.WriteStr=WriteStr;
}
public WriteFile(File fl,String WriteStr)
{
this.myfile=fl;
this.WriteStr=WriteStr;
}
public void write(){
WriteTo();
}
public void write(boolean ap)
{
WriteToAppend();
}
private void WriteTo()
{
try{
DataOutputStream fw=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myfile)));
WriteStr=Unicode.ChineseToUnicode(WriteStr);
fw.writeBytes(WriteStr);
fw.close();
errstr= "成功写入文件"+myfile;
}catch(IOException e)
{
errstr= "出错!"+e;
}
}
private void WriteToAppend()
{
try{
DataOutputStream fw=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myfile,true)));
WriteStr=Unicode.ChineseToUnicode(WriteStr);
fw.writeBytes(this.WriteStr);
fw.close();
errstr= "成功写入文件"+myfile;
}catch(IOException e)
{
errstr= "出错!"+e;
}
}
public String toString(){
return errstr;
}
}
import java.io.*;
public class WriteFile
{
private File myfile=null;
private String WriteStr=null;
private String errstr="";
public WriteFile(String filepath,String WriteStr)
{
this.myfile=new File(filepath);
this.WriteStr=WriteStr;
}
public WriteFile(File fl,String WriteStr)
{
this.myfile=fl;
this.WriteStr=WriteStr;
}
public void write(){
WriteTo();
}
public void write(boolean ap)
{
WriteToAppend();
}
private void WriteTo()
{
try{
DataOutputStream fw=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myfile)));
WriteStr=Unicode.ChineseToUnicode(WriteStr);
fw.writeBytes(WriteStr);
fw.close();
errstr= "成功写入文件"+myfile;
}catch(IOException e)
{
errstr= "出错!"+e;
}
}
private void WriteToAppend()
{
try{
DataOutputStream fw=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myfile,true)));
WriteStr=Unicode.ChineseToUnicode(WriteStr);
fw.writeBytes(this.WriteStr);
fw.close();
errstr= "成功写入文件"+myfile;
}catch(IOException e)
{
errstr= "出错!"+e;
}
}
public String toString(){
return errstr;
}
}
|
package com.inrun.webapp.bean;
import java.sql.*;
import java.io.*;
/**
* Insert the type's description here.
* Creation date: (2002-3-27 14:59:07)
* @author: Administrator
*/
public class opendb {
//定义连接字符传jdbc-odbc桥
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String SCon="jdbc:odbc:db1";
Connection conn=null;
ResultSet rs=null;
Statement stmt;
//定义打开数据库连接程序
public opendb(){
try {
Class.forName(sDBDriver);
}
//异常处理
catch(java.lang.ClassNotFoundException e){
System.err.println("opendb():"+e.getMessage());
}n
}
//定义sql查询语句
public ResultSet executeQuery(String sql){
rs=null;
try{
conn=DriverManager.getConnection(SCon);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
//异常处理
catch(SQLException ex){
}
return rs;
};
public boolean executeUpdate(String sql)
{
try
{
conn=DriverManager.getConnection(SCon);
stmt=conn.createStatement();
conn.setAutoCommit(true);
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
return false;
}
return true;
}
}
import java.sql.*;
import java.io.*;
/**
* Insert the type's description here.
* Creation date: (2002-3-27 14:59:07)
* @author: Administrator
*/
public class opendb {
//定义连接字符传jdbc-odbc桥
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String SCon="jdbc:odbc:db1";
Connection conn=null;
ResultSet rs=null;
Statement stmt;
//定义打开数据库连接程序
public opendb(){
try {
Class.forName(sDBDriver);
}
//异常处理
catch(java.lang.ClassNotFoundException e){
System.err.println("opendb():"+e.getMessage());
}n
}
//定义sql查询语句
public ResultSet executeQuery(String sql){
rs=null;
try{
conn=DriverManager.getConnection(SCon);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
//异常处理
catch(SQLException ex){
}
return rs;
};
public boolean executeUpdate(String sql)
{
try
{
conn=DriverManager.getConnection(SCon);
stmt=conn.createStatement();
conn.setAutoCommit(true);
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
return false;
}
return true;
}
}