Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1148384
  • 博文数量: 290
  • 博客积分: 3468
  • 博客等级: 中校
  • 技术积分: 3461
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-28 22:21
文章分类

全部博文(290)

文章存档

2016年(13)

2015年(3)

2014年(42)

2013年(67)

2012年(90)

2011年(75)

分类: Java

2012-04-09 11:26:40

系统环境:ubuntu 10.04+openjdk-6

关于java如何访问 sqlite3 请查这里

CollectionInfo.java

点击(此处)折叠或打开

  1. import java.applet.Applet;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.Vector;

  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JPanel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTable;
  14. import javax.swing.table.DefaultTableModel;

  15. public class CollectionInfo extends Applet implements ActionListener
  16. {
  17.     JButton recvBt = new JButton("实时接收");
  18.     JButton pauseBt = new JButton("暂停接收");
  19.     JButton searchIdBt = new JButton("查询ID");
  20.     JButton searchAllBt = new JButton("查询所有");
  21.     JButton deleteIdBt = new JButton("删除ID");
  22.     JButton deleteAllBt = new JButton("删除所有");
  23.     JTable dataTable = new JTable();
  24.     JScrollPane sp = new JScrollPane(dataTable);
  25.     DefaultTableModel dtm = null;
  26.     
  27.     JPanel p1 = new JPanel();
  28.     JPanel p2 = new JPanel();
  29.     JPanel p3 = new JPanel();
  30.     JPanel p4 = new JPanel();
  31.     
  32.     String idNum = new String("test");
  33.     
  34.     DataBase_OP d_op = null;
  35.     
  36.     Thread tsThread=null;
  37.     
  38. //    public CollectionInfo()
  39.     public void init()
  40.     {
  41.         setComponentSize();

  42.         setLayout();
  43.         
  44.         addData("2012","1234","2012-04-06 12:30:45");
  45.         regMouseEvent();
  46.         this.setVisible(true);
  47.     }
  48.     
  49.     public void setComponentSize()
  50.     {
  51.         Dimension d = new Dimension(100,60);
  52.         recvBt.setPreferredSize(d);    
  53.         pauseBt.setPreferredSize(d);
  54.         searchIdBt.setPreferredSize(d);
  55.         searchAllBt.setPreferredSize(d);
  56.         deleteIdBt.setPreferredSize(d);
  57.         deleteAllBt.setPreferredSize(d);
  58.         
  59.         Dimension d1 = new Dimension(380,400);
  60.         this.dataTable.setPreferredSize(d1);
  61.         
  62.         Dimension d2 = new Dimension(400,400);
  63.         this.sp.setPreferredSize(d2);
  64. //        recvBt.setBackground(Color.blue);
  65.         
  66. //        int rowCount = 10;
  67. //        dtm.setNumRows(rowCount);
  68.     }
  69.     
  70.     public void setLayout()
  71.     {
  72.         p1.setLayout(new BorderLayout(10,10));
  73.         p1.add(recvBt,BorderLayout.NORTH);
  74.         p1.add(pauseBt,BorderLayout.CENTER);
  75.         p1.add(searchIdBt,BorderLayout.SOUTH);
  76.         
  77.         p2.setLayout(new BorderLayout(10,10));
  78.         p2.add(searchAllBt,BorderLayout.NORTH);
  79.         p2.add(deleteIdBt,BorderLayout.CENTER);
  80.         p2.add(deleteAllBt,BorderLayout.SOUTH);
  81.         
  82.         p3.setLayout(new BorderLayout(10,10));
  83.         p3.add(p1,BorderLayout.NORTH);
  84.         p3.add(p2,BorderLayout.CENTER);        

  85.         setTable();
  86.         
  87.         p4.setLayout(new FlowLayout(FlowLayout.CENTER,10,30));
  88.         p4.add(p3);
  89.         
  90.         p4.add(sp);
  91.         
  92.         this.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));    
  93.         add(p4);
  94.     }

  95.     
  96.     public void regMouseEvent()
  97.     {
  98.         this.recvBt.addActionListener(this);
  99.         this.pauseBt.addActionListener(this);
  100.         this.searchIdBt.addActionListener(this);
  101.         this.searchAllBt.addActionListener(this);
  102.         this.deleteIdBt.addActionListener(this);
  103.         this.deleteAllBt.addActionListener(this);
  104.     }
  105.     
  106.     public void actionPerformed(ActionEvent e)
  107.     {
  108.         String args = e.getActionCommand();
  109.         if (args == "实时接收")
  110.         {
  111.             this.runtimeDisplay();
  112.         }
  113.         else if (args == "暂停接收")
  114.         {
  115.             this.pauseDisplay();
  116.         }
  117.         else if (args == "查询ID")
  118.         {
  119.             this.searchById();
  120.         }
  121.         else if (args == "查询所有")
  122.         {
  123.             this.searchAll();
  124.         }
  125.         else if (args == "删除ID")
  126.         {
  127.             this.deleteById();
  128.         }
  129.         else if (args == "删除所有")
  130.         {
  131.             this.deleteAll();
  132.         }
  133.     }
  134.     
  135.     public void runtimeDisplay()
  136.     {
  137.         System.out.println("hello,it's runtime clicked\n");
  138.     }
  139.     
  140.     public void pauseDisplay()
  141.     {
  142.         System.out.println("hello,it's pause clicked\n");
  143.     }
  144.     
  145.     public void searchById()
  146.     {
  147.         System.out.println("hello,it's searchById clicked\n");
  148.         
  149. //        inputDialog myDialog = new inputDialog();
  150. //        this.idNum = myDialog.getValue();
  151. //        System.out.println("CollectionInfo: " + idNum);
  152.         
  153.         String s1 = JOptionPane.showInputDialog(null,"请输入ID:","输入对话框",JOptionPane.PLAIN_MESSAGE);
  154.         this.clearTable();
  155.         d_op = new DataBase_OP(this);
  156.         d_op.searchById(s1);
  157.     }
  158.     
  159.     public void searchAll()
  160.     {
  161.         System.out.println("hello,it's searchAll clicked\n");
  162.         this.clearTable();
  163.         
  164.         d_op = new DataBase_OP(this);
  165.         d_op.searchAll();
  166.     }
  167.     
  168.     public void deleteById()
  169.     {
  170.         System.out.println("hello,it's deleteById clicked\n");
  171.         this.clearTable();
  172.     }
  173.     
  174.     public void deleteAll()
  175.     {
  176.         System.out.println("hello,it's deleteAll clicked\n");
  177.         this.clearTable();
  178.     }
  179.     
  180.     
  181.     public void setTable()
  182.     {
  183. //        Vector vCdata = new Vector();
  184. //        vCdata.add("ID");
  185. //        vCdata.add("数据");
  186. //        vCdata.add("时间");
  187. //        
  188. //        dataTable.setModel(dtm);
  189. //        
  190. //        for (int i = 0;i < vCdata.size();i++)
  191. //            dtm.addColumn((String)vCdata.elementAt(i));
  192.         
  193.         Object head[] = {"ID","数据","时间"};
  194.         dtm = new DefaultTableModel(head,0);
  195.         dataTable.setModel(dtm);
  196.             
  197.         dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  198.         dataTable.getColumn(head[0]).setPreferredWidth(100);
  199.         dataTable.getColumn(head[1]).setPreferredWidth(100);
  200.         dataTable.getColumn(head[2]).setPreferredWidth(180);
  201.     }
  202.     
  203.     public void addData(String id,String data,String time)
  204.     {
  205.         Vector<String> vRdata = new Vector<String>();    
  206.         vRdata.add(id);
  207.         vRdata.add(data);
  208.         vRdata.add(time);
  209.         
  210.         dtm.addRow(vRdata);        
  211.         
  212. //        dtm.setValueAt(vRdata, 0, 0);
  213.     }
  214.     
  215.     public void clearTable()
  216.     {
  217.         int i = this.dtm.getRowCount();
  218.         for (;i > 0;i--)
  219.         {
  220.             System.out.println(i);
  221.             this.dtm.removeRow(i-1);
  222.         }    
  223.     }
  224.     
  225. //    public static void main(String args[])
  226.     public void start()
  227.     {    
  228.         CollectionInfo ci = new CollectionInfo();
  229.         ci.setSize(600, 500);
  230. //        ci.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  231.     }
  232.     
  233. }


DataBase_OP.java


点击(此处)折叠或打开

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;

  6. public class DataBase_OP implements Runnable
  7. {
  8.     Thread tsThread=null;
  9.     CollectionInfo tmpInfo;
  10.     
  11.     public DataBase_OP(CollectionInfo myInfo)
  12.     {
  13.         System.out.println("it's database_op");
  14.         tmpInfo = myInfo;
  15.     }
  16.     
  17.     public Connection getConnection()
  18.     {
  19.         try
  20.         {
  21.             Class.forName("org.sqlite.JDBC");
  22.             Connection conn = DriverManager.getConnection("jdbc:sqlite:data.db");
  23.             return conn;
  24.         }
  25.         catch(Exception e)
  26.         {
  27.             System.out.println("123exception");
  28.             e.printStackTrace();
  29.             return null;
  30.         }
  31.         
  32.     }
  33.     
  34.     public void searchAll()
  35.     {
  36.         try
  37.         {
  38.             Connection conn = getConnection();
  39.             Statement stat = conn.createStatement();
  40.             ResultSet rs = stat.executeQuery("select * from save_data;");
  41.             int i = 0;
  42.             while (rs.next())
  43.             {
  44.                 i++;
  45.                 System.out.print("ID:" +i);
  46.                 System.out.print("num: " + rs.getString("num"));
  47.                 System.out.print("id: " + rs.getString("id"));
  48.                 System.out.print("data: " + rs.getString("data"));
  49.                 System.out.println("time: " + rs.getString("time"));
  50.                 tmpInfo.addData(rs.getString("id"), rs.getString("data"), rs.getString("time"));
  51.             }
  52.             
  53.             rs.close();
  54.             stat.close();
  55.             conn.close();
  56.         }
  57.         catch(Exception e)
  58.         {
  59.             e.printStackTrace();
  60.         }
  61.     }
  62.     
  63.     public void searchById(String id)
  64.     {
  65.         try
  66.         {
  67.             Connection conn = getConnection();
  68.             PreparedStatement stat = conn.prepareStatement("select * from save_data where id=?;");
  69.             stat.setString(1, id);
  70.             ResultSet rs = stat.executeQuery();
  71.             int i = 0;
  72.             while (rs.next())
  73.             {
  74.                 i++;
  75.                 System.out.print("ID:" +i);
  76.                 System.out.print("num: " + rs.getString("num"));
  77.                 System.out.print("id: " + rs.getString("id"));
  78.                 System.out.print("data: " + rs.getString("data"));
  79.                 System.out.println("time: " + rs.getString("time"));
  80.                 tmpInfo.addData(rs.getString("id"), rs.getString("data"), rs.getString("time"));
  81.             }
  82.             
  83.             rs.close();
  84.             stat.close();
  85.             conn.close();
  86.         }
  87.         catch(Exception e)
  88.         {
  89.             e.printStackTrace();
  90.         }
  91.     }
  92.     
  93.     
  94.     public void deleteAll()
  95.     {
  96.         try
  97.         {
  98.             Connection conn = getConnection();
  99.             Statement stat = conn.createStatement();
  100.             stat.executeUpdate("delete from save_data;");
  101.     
  102.             stat.close();
  103.             conn.close();
  104.             
  105.             this.searchAll();
  106.         }
  107.         catch(Exception e)
  108.         {
  109.             e.printStackTrace();
  110.         }
  111.     }
  112.     
  113.     public void deleteById(String id)
  114.     {
  115.         try
  116.         {

  117.             Connection conn = getConnection();
  118.             PreparedStatement stat = conn.prepareStatement("delete from save_data where id=?;");
  119.             stat.setString(1, id);
  120.             stat.executeUpdate();
  121.             
  122.             stat.close();
  123.             conn.close();
  124.             
  125.             this.searchAll();
  126.         }
  127.         catch(Exception e)
  128.         {
  129.             e.printStackTrace();
  130.         }
  131.     }
  132.     
  133.     public void insert(String id,String data,String time)
  134.     {
  135.         try
  136.         {
  137.             Connection conn = this.getConnection();
  138.             PreparedStatement stat = conn.prepareStatement("insert into save_data values(null,?,?,?);");
  139.             stat.setString(1, id);
  140.             stat.setString(2,data);
  141.             stat.setString(3, time);
  142.             stat.executeUpdate();
  143.             
  144.             stat.close();
  145.             conn.close();
  146.             
  147.             this.searchById(id);
  148.         }
  149.         catch (Exception e)
  150.         {
  151.             e.printStackTrace();
  152.         }
  153.     }

  154.     public void createThread()
  155.     {
  156.         tsThread = new Thread(this,"db");
  157.         tsThread.start();
  158.     }
  159.     
  160.     public void run()
  161.     {
  162.         if (tsThread != null)
  163.         {
  164.             this.searchAll();
  165.         }
  166.     }
  167. /*    
  168.     public static void main(String args[])
  169.     {
  170.         DataBase_OP ts = new DataBase_OP();

  171. //        ts.createThread();
  172. //        ts.deleteAll();
  173.         ts.insert("2015", "1234", "2012-04-06");
  174. //        ts.searchById("save_data","2012");
  175. //        ts.deleteById("2012");

  176.     }
  177. */
  178. }
index.html

点击(此处)折叠或打开

  1. <HTML>
  2. <HEAD>
  3. <TITLE>
  4. test
  5. </TITLE>
  6. </HEAD>
  7. <BODY>
  8. <center>
  9. <h3>
  10. test
  11. </h3>
  12. <BR>
  13. <APPLET
  14. CODEBASE = "."
  15. CODE = "CollectionInfo.class"
  16. NAME = "collection info"
  17. WIDTH = 600
  18. HEIGHT = 500
  19. HSPACE = 0
  20. VSPACE = 0
  21. ALIGN = middle
  22. >
  23. </APPLET>
  24. </center>
  25. </BODY>
  26. </HTML>

运行(上面程序如果改成继承至 JFrame 运行正常)
$ appletviewer index.html  //点击查询数据时,出错

java.lang.ClassNotFoundException: org.sqlite.JDBC
java.lang.NullPointerException

解决方法:
解压 sqlite-jdbc-3.6.23.1-beta-1-SNAPSHOT.jar 到与 index.html 同一目录
或 把sqlite-jdbc-3.6.23.1-beta-1-SNAPSHOT.jar 放至$JRE_HOME/lib/ext(/usr/lib/jvm/java-6-openjdk/jre/lib/ext)

之后依次出错如下错误:
java.security.AccessControlException: access denied (java.util.PropertyPermission user.dir read)

java.security.AccessControlException: access denied (java.io.FilePermission /home/loongson/workspace/collectionInfo/src/sqlite read)

java.security.AccessControlException: access denied (java.io.FilePermission /home/loongson/workspace/collectionInfo/src/sqlite/data.db write)

解决方法:
在当前目录建立名为testPol 的文件 内容如下

点击(此处)折叠或打开

  1. grant
  2. {
  3.     permission java.util.PropertyPermission "user.dir", "read";
  4.     permission java.io.FilePermission "/home/loongson/workspace/collectionInfo/src/sqlite", "read";
  5.     permission java.io.FilePermission "/home/loongson/workspace/collectionInfo/src/sqlite/data.db", "write";
  6. };
将其添加到 java.security 文件中

点击(此处)折叠或打开

  1. $ sudo vi /usr/lib/jvm/java-6-openjdk/jre/lib/security/java.security
 在
 97 policy.url.1=file:${java.home}/lib/security/java.policy
 98 policy.url.2=file:${user.home}/.java.policy  
 的下面添加
 99 policy.url.3=file:/home/loongson/workspace/collectionInfo/src/sqlite/testPol
//即将testPol 的所在路径添加到其中

$ appletviewer index.html //访问数据库正常

参考链接:
http://www.chinaunix.net/jh/26/256602.html

以下
参考链接中的主要内容:
首先,DbaAppl.java中_url串指定的是与Web Server在同一台主机上(server1)的Oracle,编译后把DbaAppl.class和DbaAppl.html
一起放在Web Server的同一目录下,在客户机的浏览器里访问DbaAppl.html,查看Java控制台,得到如下信息

Cannot find driver
oracle.jdbc.driver.OracleDriver
java.lang.NullPointerException

这一出错信息的意思是,DriverManager 已在 applet HTML和类文件所在的目录以及当前JRE使用的本地CLASSPATH中没有找到 JDBC 驱动程序。
解决办法,将JDBC 驱动程序oracle-jdbc-driver.jar文件放在客户机当前使用的JRE的${JRE_HOME}/lib /ext目录下,或者用jar工具将oracle-jdbc-driver.jar解压,把解压后得到的oracle目录也放在Web Server上 applet所在
的目录中。

更正后,重新访问DbaAppl.html,查看Java控制台,得到如下信息

java.security.AccessControlException: access denied(java.net.SocketPermission server1 resolve)

这个错误的通知你访问被拒绝。这就是说,由于 applet 程序试 图在没有获得正确的权限的情况下访问系统资源,括号中代码表示,若要纠正这 种情况,您需要一个向数据库所在计算机(主机名为server1) 授予 applet 访问权限的 SocketPermission。


解决办法,利用 Policy 工具生成你所需要的策略文件,或者用一个 ASCII 编辑 器生成该策略文件。
下面的代码是名为 DbaApplPol 的、获得上述权限的策略文件的内容

grant {
  permission java.net.SocketPermission "server1", "resolve";
};

创建了策略文件后,还要修改${JRE_HOME}/lib/security/java.security文件

policy.url.1=file:${java.home}/lib/security/java.policy 
policy.url.2=file:${user.home}/.java.policy 

下添加一行
policy.url.3=file:/path/to/DbaApplPol 


再次访问DbaAppl.html,成功运行。

再将DbaAppl.java中_url串改为指定另一台主机(server2)上的Oracle,编译,上传,访问得到如下错误信息

java.security.AccessControlException: access denied(java.net.SocketPermission 10.6.1.16:1521 connect,resolve)

这回的意思是需要一个允许在数据库所在的server2上访问IP地址和端口的SocketPermission。在前面生成的策略文件中加入

permission java.net.SocketPermission "10.6.1.16:1521", "connect, resolve";

后,成功运行。

总结一下,通过applet访问数据库不是不可以的,但确实不太方便,某些特殊环境下可能值得使用。
[list=1]
[*]客户端要有JRE支持,可以在客户机访问applet所在页面是提示安装Java Plug-in,这样的页面可以通过HTMLConvert工具生成
[*]JDBC驱动要么放在客户机JRE的CLASSPATH下,要么与applet放在一起,注意如果驱动是jar文件的话,要解开
[*]通过设置正确的策略文件,applet不仅可以访问与自己在同一台机器上的数据库,也可以访问其他远程数据库。不过,需要每个想要成功运行applet的客户机修改其本地JRE的java.security文件


阅读(986) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~