JDBC-MySQL代码例子-6

/* Author: Tay Xiang Ren/Chua Kian Siong(added rmi code)
 * Date last updated: 02/04/10
 * Company/Subsystem: MSI/DB/RMI
 *
 * Title: Implementation of trackerInterface. Contains full methods that tracker will call from
 *    db.
 *
 */

   import java.io.*;
   import java.rmi.*;
   import java.rmi.server.UnicastRemoteObject;
   import java.util.*;
   import java.sql.*;
   import java.io.*;
   import java.lang.*;
   import java.io.File;
   import java.util.logging.Level;
   import java.util.logging.Logger;
   import javax.swing.JEditorPane;
   import javax.xml.parsers.*;
   import javax.xml.transform.*;
   import javax.xml.transform.dom.*;
   import javax.xml.transform.stream.*;
   import org.w3c.dom.*;


    public class tracker_dbImpl extends UnicastRemoteObject implements tracker_dbInterface
   {
       static PrintStream p=null;
       static boolean flag = false;
       static Connection con=null;
       static JEditorPane editorPane =null;
       public tracker_dbImpl() throws RemoteException{
           getConnection();
       }
       public tracker_dbImpl(PrintStream p,JEditorPane editorPane) throws RemoteException{
           this.p=p;
           this.editorPane=editorPane;
           getConnection();
       }
       
       public boolean storeTrackerLog(String mes) throws RemoteException,SQLException {
           try {
               StringTokenizer tkrStrToken = new StringTokenizer(mes,",");
               while (tkrStrToken.hasMoreTokens()) {
                   String methodInvoked = tkrStrToken.nextToken();
                   String callFrom = tkrStrToken.nextToken();
                   String callTo = tkrStrToken.nextToken();
                   String timestamp = tkrStrToken.nextToken();
                   p.println(editorPane.getText()+"/n"+timestamp+"     Method: "+methodInvoked+" calledFrom: "+callFrom+" callTo: "+callTo);
                   p.println();
                   editorPane.setText(editorPane.getText()+"/n"+timestamp+"     Method: "+methodInvoked+" calledFrom: "+callFrom+" callTo: "+callTo);
               }
                return true;
           }  
           catch (Exception e) {
               e.printStackTrace();
               return false;
           }
       }
  
   //public void setFlag() {
       //flag = true;
   //}

       public void sendMap() throws SQLException, FileNotFoundException, IOException {
            //if (flag == true) {
                PreparedStatement psmnt=con.prepareStatement("select map_selecting from setup");
                ResultSet rs=psmnt.executeQuery();
                rs.next();
                String map_selecting=rs.getString("map_selecting");
                System.out.println("=============================================="+map_selecting+"?in sendMap");
                psmnt.close();

                psmnt=con.prepareStatement("select map_binary from map where map_name=/""+map_selecting+"/"");
                rs=psmnt.executeQuery();
                while(rs.next()){
                    Blob blob=rs.getBlob("map_binary");
                    File file=new File("D://CPE207//tracker//"+map_selecting+".udk");
                    FileOutputStream outStream=new FileOutputStream(file);
                    InputStream inStream=blob.getBinaryStream();

                    int length=-1;
                    int size=(int)blob.length();
                    byte [] bytefile=new byte[size];

                    while ((length = inStream.read(bytefile)) != -1)
                    {
                        outStream.write(bytefile, 0, length);
                        outStream.flush();
                    }

                    inStream.close();
                    outStream.close();
                }
                rs.close();
                psmnt.close();
          //flag = false;
 //}
 //else
            //System.out.println("Map is not selected");
    }
 
        public String getFileName() throws RemoteException,SQLException{
                 PreparedStatement psmnt=con.prepareStatement("select map_selecting from setup");
                 ResultSet rs=psmnt.executeQuery();
                 rs.next();
                 String map_selecting=rs.getString("map_selecting");
                 if(rs.isLast()){
                     rs.close();
                     psmnt.close();
                     return map_selecting;
                 }
                 rs.close();
                 psmnt.close();
                 return null;
        }

        public InputStream getInputStream(File f) throws IOException
 {
            try {
                sendMap();
            } catch (SQLException ex) {
                Logger.getLogger(tracker_dbImpl.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(tracker_dbImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            return new RMIInputStream(new RMIInputStreamImpl(new
            FileInputStream(f)));
 }

    public static void getConnection()
    {
        String connectionURL="jdbc:mysql://127.0.0.1:3306/PBSS";
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection(connectionURL, "root","instance");
            System.out.println("Tracker Connected successfully!/n");
        }
        catch(Exception ex){
            System.out.println("Found some error: "+ex);
        }
     }

   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值