------------建一个javabean 类 WorkRecord 用于保存访问数据库记录的内容
public class WorkRecord
{
private long do_seq;
private String proc_name;
private String sql_desc;
private String work_status;
public void setdo_seq(long do_seq)
{
this.do_seq=do_seq;
}
public void setproc_name(String proc_name)
{
this.proc_name=proc_name;
}
public void setsql_desc(String sql_desc)
{
this.sql_desc=sql_desc;
}
public void setwork_status(String work_status)
{
this.work_status=work_status;
}
public long getDo_seq()
{
return do_seq;
}
public String getProc_name()
{
return proc_name;
}
public String getSql_desc()
{
return sql_desc;
}
public String getWork_status()
{
return work_status;
}
public static void main (String[] args)
{
System.out.println("hello");
}
}
------------------------------建一个javabean类faq 用于访问数据库,并将访问的记录存于对象WorkRecord中,多个WorkRecord存于列表中
//java bean
//file name: faq.java
//package Test;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
//import WorkRecord;
import java.sql.*;
public class faq {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:wz";
Connection conn = null;
ResultSet rs = null;
JSON json;
public faq() {
try {
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e) {
System.err.println("faq(): " + e.getMessage());
}
}
public void executeQuery(String sql)
{
rs = null;
try {
conn = DriverManager.getConnection(sConnStr,"liuyz","liuyz1");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//List result =new ArrayList();
List<WorkRecord> result = new ArrayList<WorkRecord>();
while (rs.next())
{
WorkRecord rec= new WorkRecord();
rec.setdo_seq(rs.getLong("do_seq"));
rec.setproc_name(rs.getString("proc_name"));
rec.setsql_desc(rs.getString("sql_desc"));
rec.setwork_status(rs.getString("work_status"));
result.add(rec);
}
//JSONObject json= JSONSerializer.toJSON(result);
json = JSONSerializer.toJSON(result);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
//return json.toString();
System.out.println(json.toString());
}
public static void main (String[] args)
{
faq aaa =new faq();
aaa.executeQuery("select * from work_log where do_seq=399252 or do_seq=399261 ");
}
}
1、在c:\下解压json支持库生成 org net 目录
2、在c:\下编写前面两个文件
3、javac WorkRecord.java ; javac faq.java
4、java faq 运行后返回以下json 格式的数据
[{"do_seq":399252,"proc_name":"LIUYZ.GET_GD\n","sql_desc":"delete from busi_log
a where\nbusiness_id in (500000020057,8000100108,500000020204,800015100004,80
0015100003,800015100002,800015100008,800015100005,800015100006,800015100000,8000
100100,8000100126,500000020055,500000020053,8000000015,8000000012,800015101178,5
00000021011,800015101251,500000021012,500000020222,500000020223,500000020038,500
000020093,500000020094,360,500000020064,500000020339,500000020079,500000020009,5
00000020006,2100000003,8000100103,500000020028,500000020007,500000020012,5000000
20221,500000020220,500000020073,500000020074,8000000620,500000020320,50000002020
3,900000030001,500000020076,500000020077,8000000021,8100000987,500000020008,5000
00020096,2100000004,8000000002,8000000005)","work_status":"finished"},{"do_seq":
399261,"proc_name":"LIUYZ.GET_GD\n","sql_desc":"delete from yz_gd where fee_de
tail like '%终端差价%'","work_status":"finished"}]
注:
//List result =new ArrayList();
List<WorkRecord> result = new ArrayList<WorkRecord>();
编译正常
如改为
List result =new ArrayList();
// List<WorkRecord> result = new ArrayList<WorkRecord>();
编译出现下面信息
C:\>javac faq.java
注意:faq.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。
但运行正常,仍能产生json格式的数据