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);
        }
     }

   }

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭