Archive

Archive for the ‘SPO’ Category

Bash: count all lines in a directory

November 15th, 2009 karlosp No comments
CODE:
  1. find . -type f | xargs wc -l | grep 'total' | awk '{print $1}'

Categories: SPO Tags:

Using JSSE for secure socket communication

July 28th, 2009 karlosp No comments
Categories: Java Tags:

Java Threads

May 17th, 2009 karlosp No comments

http://www.exampledepot.com/egs/java.lang/BasicThread.html

Categories: Java Tags:

Java JTable mastering

April 28th, 2009 karlosp No comments
JAVA:
  1. //My custom cell editor
  2. import java.awt.Color;
  3. import java.awt.Component;
  4.  
  5. import javax.swing.BorderFactory;
  6. import javax.swing.DefaultCellEditor;
  7. import javax.swing.JComboBox;
  8. import javax.swing.JTable;
  9. import javax.swing.JTextField;
  10.  
  11. public class MyCellEditor extends DefaultCellEditor{
  12. private static final long serialVersionUID = 1L;
  13.  
  14.  
  15. public MyCellEditor() {
  16. super(new javax.swing.JTextField());
  17. }
  18.  
  19. public Component getTableCellEditorComponent(JTable table, Object value,
  20. boolean isSelected, int row, int column){
  21.  
  22. Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column);
  23.  
  24. if(c instanceof JTextField){
  25. jtf = (JTextField)c;
  26. jtf.selectAll();
  27. jtf.setBorder(BorderFactory.createLineBorder(Color.GREEN, 1));
  28. }
  29.  
  30. return c;
  31. }
  32. }

JAVA:
  1. /*
  2. * TableTest.java
  3. *
  4. * Created on 08 July 2004, 11:07
  5. */
  6.  
  7. /**
  8. *
  9. * @author  admin
  10. */
  11. import javax.swing.JTable;
  12.  
  13. class MyEditor extends javax.swing.DefaultCellEditor{
  14.  
  15. javax.swing.JTextField jtf;
  16.  
  17. public MyEditor(){
  18. super(new javax.swing.JTextField());
  19.  
  20. }
  21.  
  22. public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){
  23. java.awt.Component c = super.getTableCellEditorComponent( table, value, isSelected, row, column);
  24. if(c instanceof javax.swing.JTextField){
  25. jtf = ((javax.swing.JTextField)c);
  26. jtf.selectAll();
  27. //jtf.setText("");
  28. //jtf.setCaretPosition(0);
  29. jtf.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.BLACK, 1));
  30. }
  31.  
  32. return c;
  33. }
  34.  
  35. }
  36. public class TableTest extends javax.swing.JFrame {
  37.  
  38. /** Creates new form TableTest */
  39. public TableTest() {
  40. initComponents();
  41. ((javax.swing.DefaultCellEditor)jTable1.getDefaultEditor(new Object().getClass())).setClickCountToStart(1);
  42.  
  43. //jTable1.setDefaultEditor(new Object().getClass(), new MyEditor());
  44. jTable2.setDefaultEditor(new Object().getClass(), new MyEditor());
  45.  
  46. }
  47.  
  48. /** This method is called from within the constructor to
  49. * initialize the form.
  50. * WARNING: Do NOT modify this code. The content of this method is
  51. * always regenerated by the Form Editor.
  52. */
  53. private void initComponents() {
  54. jTable1 = new javax.swing.JTable();
  55. jTable2 = new javax.swing.JTable();
  56. jLabel1 = new javax.swing.JLabel();
  57.  
  58. addWindowListener(new java.awt.event.WindowAdapter() {
  59. public void windowClosing(java.awt.event.WindowEvent evt) {
  60. exitForm(evt);
  61. }
  62. });
  63.  
  64. jTable1.setModel(new javax.swing.table.DefaultTableModel(
  65. new Object [][] {
  66. {"jtable1", "jtable1", "jtable1", "jtable1"},
  67. {"jtable1", "jtable1", "jtable1", "jtable1"},
  68. {"jtable1", "jtable1", "jtable1", "jtable1"},
  69. {"jtable1", "jtable1", "jtable1", "jtable1"}
  70. },
  71. new String [] {
  72. "Title 1", "Title 2", "Title 3", "Title 4"
  73. }
  74. ));
  75. getContentPane().add(jTable1, java.awt.BorderLayout.NORTH);
  76.  
  77. jTable2.setModel(new javax.swing.table.DefaultTableModel(
  78. new Object [][] {
  79. {"jtable2", "jtable2", "jtable2", "jtable2"},
  80. {"jtable2", "jtable2", "jtable2", "jtable2"},
  81. {"jtable2", "jtable2", "jtable2", "jtable2"},
  82. {"jtable2", "jtable2", "jtable2", "jtable2"}
  83. },
  84. new String [] {
  85. "Title 1", "Title 2", "Title 3", "Title 4"
  86. }
  87. ));
  88. getContentPane().add(jTable2, java.awt.BorderLayout.SOUTH);
  89.  
  90. jLabel1.setText("JTable clear selection on focus loss example by cjard@hotmail.com");
  91. getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);
  92.  
  93. pack();
  94. }
  95.  
  96. /** Exit the Application */
  97. private void exitForm(java.awt.event.WindowEvent evt) {
  98. System.exit(0);
  99. }
  100.  
  101. /**
  102. * @param args the command line arguments
  103. */
  104. public static void main(String args[]) {
  105. new TableTest().show();
  106. }
  107.  
  108. // Variables declaration - do not modify
  109. private javax.swing.JLabel jLabel1;
  110. private javax.swing.JTable jTable1;
  111. private javax.swing.JTable jTable2;
  112. // End of variables declaration
  113.  
  114. }

Categories: Java Tags: , ,

Java custom logger

April 21st, 2009 karlosp 1 comment
JAVA:
  1. import java.util.logging.*;
  2.  
  3. public class MyLogger {
  4.  
  5. //1. CREATE logger
  6. //private static final Logger dnevnik = Logger.getLogger("bla");
  7.  
  8. //2. Init MyLogger
  9. //MyLogger.init();
  10.  
  11. //3. [Switch Off]
  12. //dnevnik.setLevel(Level.OFF);
  13.  
  14. //4. Usage
  15. //dnevnik.info("Vstop v metodo bla bla čšž fasf\n");
  16.  
  17. public static void init()
  18. {
  19. if(System.getProperty("java.util.logging.config.class") == null &&
  20. System.getProperty("java.util.logging.config.class") == null){
  21.  
  22. try {
  23. String currentDir = System.getProperty("user.dir");
  24. System.out.println("Logger Working Directory: " + currentDir);
  25.  
  26. Logger lg = Logger.getLogger("");
  27.  
  28. lg.setLevel(Level.ALL);
  29. FileHandler fh = new FileHandler(currentDir+"\\program%g.log", 0, 10);
  30. fh.setEncoding("UTF-8");
  31.  
  32. lg.addHandler(fh);
  33. } catch (Exception e) {
  34. System.out.printf("Nisem uspel vspostaviti dnevnika!\ne = %s",e);
  35. }
  36. }
  37. }
  38.  
  39. public static void main(String[] args) {
  40. init();
  41. }
  42. }

Categories: Java Tags: ,

Java MySql connection (povezava)

June 23rd, 2008 karlosp No comments

Copy mysql-connector-java-3114-bin to C:\Program Files\Java\jre1.6.X\lib\ext

JAVA:
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5. // Notice, do not import com.mysql.jdbc.*
  6. // or you will have problems!
  7.  
  8. public class LoadDriver {
  9.  
  10.     Connection conn = null;
  11.    
  12.     public static void main(String[] args) {
  13.         try {
  14.         // The newInstance() call is a work around for some
  15.         // broken Java implementations
  16.  
  17.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  18.         conn =
  19.         DriverManager.getConnection("jdbc:mysql://localhost/test?" +
  20.                                    "user=monty&password=greatsqldb");
  21.                                   
  22.         Statement stmt = null;
  23.         ResultSet rs = null;
  24.  
  25.         stmt = conn.createStatement();
  26.         rs = stmt.executeQuery("SELECT foo FROM bar");
  27.  
  28.         // or alternatively, if you don't know ahead of time that
  29.         // the query will be a SELECT...
  30.  
  31.         if (stmt.execute("SELECT foo FROM bar")) {
  32.             rs = stmt.getResultSet();
  33.         }
  34.  
  35.         } catch (Exception ex) {
  36.             // handle any errors
  37.             System.out.println("SQLException: " + ex.getMessage());
  38.             System.out.println("SQLState: " + ex.getSQLState());
  39.             System.out.println("VendorError: " + ex.getErrorCode());
  40.  
  41.         }
  42.         finally {
  43.         // it is a good idea to release
  44.         // resources in a finally{} block
  45.         // in reverse-order of their creation
  46.         // if they are no-longer needed
  47.  
  48.         if (rs != null) {
  49.             try {
  50.                 rs.close();
  51.             } catch (SQLException sqlEx) { } // ignore
  52.  
  53.             rs = null;
  54.         }
  55.  
  56.         if (stmt != null) {
  57.             try {
  58.                 stmt.close();
  59.             } catch (SQLException sqlEx) { } // ignore
  60.  
  61.             stmt = null;
  62.         }
  63.     }   
  64. }

JAVA:
  1. import java.sql.*;
  2.  
  3. public class Jdbc10 {
  4.   public static void main(String args[]){
  5.     System.out.println(
  6.                   "Copyright 2004, R.G.Baldwin");
  7.     try {
  8.       Statement stmt;
  9.       ResultSet rs;
  10.  
  11.       //Register the JDBC driver for MySQL.
  12.       Class.forName("com.mysql.jdbc.Driver");
  13.  
  14.       //Define URL of database server for
  15.       // database named JunkDB on the localhost
  16.       // with the default port number 3306.
  17.       String url =
  18.             "jdbc:mysql://localhost:3306/JunkDB";
  19.  
  20.       //Get a connection to the database for a
  21.       // user named auser with the password
  22.       // drowssap, which is password spelled
  23.       // backwards.
  24.       Connection con =
  25.                      DriverManager.getConnection(
  26.                         url,"auser", "drowssap");
  27.  
  28.       //Display URL and connection information
  29.       System.out.println("URL: " + url);
  30.       System.out.println("Connection: " + con);
  31.  
  32.       //Get a Statement object
  33.       stmt = con.createStatement();
  34.  
  35.       //As a precaution, delete myTable if it
  36.       // already exists as residue from a
  37.       // previous run.  Otherwise, if the table
  38.       // already exists and an attempt is made
  39.       // to create it, an exception will be
  40.       // thrown.
  41.       try{
  42.         stmt.executeUpdate("DROP TABLE myTable");
  43.       }catch(Exception e){
  44.         System.out.print(e);
  45.         System.out.println(
  46.                   "No existing table to delete");
  47.       }//end catch
  48.  
  49.       //Create a table in the database named
  50.       // myTable.
  51.       stmt.executeUpdate(
  52.             "CREATE TABLE myTable(test_id int," +
  53.                   "test_val char(15) not null)");
  54.  
  55.       //Insert some values into the table
  56.       stmt.executeUpdate(
  57.                 "INSERT INTO myTable(test_id, " +
  58.                     "test_val) VALUES(1,'One')");
  59.  
  60.       //Get another statement object initialized
  61.       // as shown.
  62.       stmt = con.createStatement(
  63.                ResultSet.TYPE_SCROLL_INSENSITIVE,
  64.                      ResultSet.CONCUR_READ_ONLY);
  65.  
  66.       //Query the database, storing the result
  67.       // in an object of type ResultSet
  68.       rs = stmt.executeQuery("SELECT * " +
  69.                 "from myTable ORDER BY test_id");
  70.  
  71.       //Use the methods of class ResultSet in a
  72.       // loop to display all of the data in the
  73.       // database.
  74.       System.out.println("Display all results:");
  75.       while(rs.next()){
  76.         int theInt= rs.getInt("test_id");
  77.         String str = rs.getString("test_val");
  78.         System.out.println("\ttest_id= " + theInt
  79.                              + "\tstr = " + str);
  80.       }//end while loop
  81.  
  82.       //Display the data in a specific row using
  83.       // the rs.absolute method.
  84.       System.out.println(
  85.                         "Display row number 2:");
  86.       if( rs.absolute(2) ){
  87.         int theInt= rs.getInt("test_id");
  88.         String str = rs.getString("test_val");
  89.         System.out.println("\ttest_id= " + theInt
  90.                              + "\tstr = " + str);
  91.       }//end if
  92.  
  93.       //Delete the table and close the connection
  94.       // to the database
  95.       stmt.executeUpdate("DROP TABLE myTable");
  96.       con.close();
  97.     }catch( Exception e ) {
  98.       e.printStackTrace();
  99.     }//end catch
  100.   }//end main
  101. }//end class Jdbc10

Categories: Java, Malo mešano Tags:

Fibonacci number: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

January 21st, 2008 karlosp No comments
CODE:
  1. #!/bin/bash
  2. if [ $# -ne 1 ]
  3. then
  4.     echo 'Uporaba: sh fibo n'
  5.     exit
  6. fi
  7.  
  8. y0=0
  9. y1=1
  10. stevec=2
  11. if [ $# -eq 1 -a $1 -gt 2 ]
  12. then
  13. echo $y0
  14. echo $y1
  15.     while [ $stevec -le $1 ]
  16.     do
  17.         let "y3=$y0+$y1"
  18.         echo $y3
  19.  
  20.         y0=$y1
  21.         y1=$y3
  22.         let "stevec=$stevec+1"
  23.     done
  24. fi

Categories: 3 letnik, Bash, Fax, SPO Tags:

Izriši 2 trikotnika in kvadrat

January 15th, 2008 karlosp No comments
JAVA:
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4.  
  5. public class Lik extends JPanel{
  6. private static Color barvaLika = new Color(0,0,255);
  7. private static int xz = 10;
  8. private static int yz = 10;
  9.  
  10.     public static void main(String[] args) {
  11.         JFrame okno = new JFrame("Lik");
  12.         Container vsebnik = okno.getContentPane();
  13.         vsebnik.setLayout(new GridLayout(1,3));
  14.        
  15.         vsebnik.add(new Trikotnik(xz,yz,80,80,10,50, barvaLika));   
  16.         vsebnik.add(new Trikotnik(xz,yz,30,30,10,50, barvaLika));
  17.         vsebnik.add(new Kvadrat(xz,yz,100,barvaLika));
  18.        
  19.        
  20.         okno.setVisible(true);
  21.         okno.setSize(400, 300);
  22.         okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.  
  24.     }
  25.  
  26. }

JAVA:
  1. import java.awt.*;
  2.  
  3.  
  4. public class Trikotnik extends Lik{
  5.     private int xz, yz, x1k, y1k, x2k, y2k;
  6.     private Color barvaL;
  7.    
  8.     public Trikotnik(int x0, int y0, int x1, int y1, int x2, int y2, Color barva) {
  9.         super();
  10.         xz = x0;
  11.         yz = y0;
  12.         x1k = x1;
  13.         y1k = y1;
  14.         x2k = x2;
  15.         y2k = y2;
  16.         barvaL = barva;
  17.     }
  18.  
  19.    
  20.      @Override
  21.     public void paint(Graphics g) {
  22.         super.paint(g);
  23.         g.setColor(barvaL);
  24.         g.drawLine(xz, yz, x1k, y1k);
  25.         g.drawLine(xz, yz, x2k, y2k);
  26.         g.drawLine(x1k, y1k, x2k, y2k);
  27.     }
  28.    
  29.      @Override
  30.     public void setSize(int width, int height) {
  31.         // TODO Auto-generated method stub
  32.         super.setSize(300, 300);
  33.     }
  34. }

JAVA:
  1. import java.awt.*;
  2.  
  3. public class Kvadrat extends Lik{
  4.     private int xz, yz, X;
  5.     private Color barvaL;
  6.    
  7.     public Kvadrat(int x0, int y0, int x, Color barva) {
  8.         super();
  9.         xz = x0;
  10.         yz = y0;
  11.         X = x;
  12.         barvaL = barva;
  13.     }
  14.    
  15.      @Override
  16.     public void paint(Graphics g) {
  17.         super.paint(g);
  18.         g.setColor(barvaL);
  19.         g.drawRect(xz, yz, Math.abs(xz-X), Math.abs(xz-X));
  20.     }
  21.    
  22.      @Override
  23.     public void setSize(int width, int height) {
  24.         // TODO Auto-generated method stub
  25.         super.setSize(300, 300);
  26.     }
  27. }

Categories: 3 letnik, Fax, Java, SPO Tags:

Preveri geslo (min 8 znakov, vsaj ena števka in vsaj en [@#$%])

January 15th, 2008 karlosp No comments
CODE:
  1. #!/bin/sh
  2. if [ $# -ne 1 ]
  3. then
  4. echo napaka pri uporabi programa
  5. exit
  6. fi
  7.  
  8. dolzina=`echo -n $1 | wc -c`
  9.  
  10. if [ $dolzina -lt 8 ]
  11. then
  12. echo Prekratko geslo
  13. exit
  14. fi
  15.  
  16. if [ `echo $1 | grep [0-9]` ]
  17. then
  18.     if [ `echo $1 | grep [@#$%]` ]
  19.     then
  20.     echo geslo OK
  21.     fi
  22. fi

Categories: 3 letnik, Bash, Fax, SPO Tags:

Izračun fakultete (!)

January 15th, 2008 karlosp No comments
CODE:
  1. #!/bin/bash
  2.  
  3. if [ $# -eq 0 ]
  4. then
  5. echo Vnesi stevilo
  6. exit
  7. fi
  8.  
  9. if [ $1 -eq 1 ]
  10. then
  11.     echo 1
  12.     exit
  13. else
  14. i=2
  15. tmp=1
  16. while true
  17. do
  18.     let tmp=$i*$tmp
  19.     if [ $i -eq $1 ]
  20.         then
  21.         echo konec: $tmp
  22.         exit
  23.     fi
  24.     let i=$i+1
  25. done
  26. fi

Categories: 3 letnik, Bash, Fax, SPO Tags: , ,

Budilka

January 14th, 2008 karlosp No comments
JAVA:
  1. import javax.swing.*;
  2.  
  3. public class Test {
  4.     public static void main(String[] args) {
  5.         int ms = 5000;
  6.        
  7.         JFrame okno = new JFrame("Ura budilka");
  8.         JLabel jl = new JLabel("Zbudi se cez "+ ms +" ms");
  9.         JPanel jp = new JPanel();
  10.         jp.add(jl);
  11.        
  12.         okno.add(jp);
  13.         okno.setVisible(true);
  14.         okno.setSize(300,300);
  15.         okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.        
  17.         new Alarm(jl, jp, ms).start();
  18.     }
  19. }

JAVA:
  1. import javax.swing.*;
  2.  
  3. public class Alarm extends Thread{
  4.     JLabel napis;
  5.     JPanel jp;
  6.     JLabel jl = new JLabel("TEST");
  7.     int milsec = 0;
  8.     Alarm(JLabel jl, JPanel j, int ms){
  9.         napis = jl;
  10.         jp = j;
  11.         milsec = ms;
  12.     }
  13.  
  14.     public void run() {
  15.             try {
  16.                 sleep(milsec);
  17.                 napis.setText("Zbudi se zdaj!!");
  18.                
  19.             } catch (InterruptedException e) {
  20.                 // TODO Auto-generated catch block
  21.                 e.printStackTrace();
  22.             }
  23.     }
  24. }

Categories: 3 letnik, Fax, Java, SPO Tags:

V C-ju napišite preprost program DIREXE

January 9th, 2008 karlosp No comments
CODE:
  1. V C-ju napišite preprost program DIREXE,
  2. ki izpiše vse datoteke v trenutnem direktoriju,
  3. katere lahko uporabnik zažene. Pri tem ne smete
  4. uporabljati klicev,
  5. ki poganjajo sistemske ukaze (kot so system(...)
  6. ali execX(...)).

C:
  1. #include<dirent .h>
  2. #include<sys /stat.h>
  3.  
  4. int main() {
  5.  
  6.   DIR *dir;              //struktura ki ponazarja directory
  7.   struct dirent *file;   //struktura file-a, ima vec podatkov file-a 
  8.   struct stat results;   //struktura ukaza stat, vec podatkov
  9.  
  10.   dir = opendir(".");
  11.    
  12.   while(file = readdir(dir)) {    //ko ni vec file-ov vrne NULL
  13.  
  14.     stat(file->d_name, &results);   //zapomnimo si podatke file-a v results(struktura stat)
  15.  
  16.     printf("\tres:%d, \n\tS_xisur:%d \n",results.st_mode,S_IXUSR);
  17.     if(results.st_mode & S_IXUSR) {   //preverimo ce je executable
  18.       printf("%s\n", file->d_name )//izpisemo
  19.     }
  20.  
  21.   }
  22.   closedir(dir);
  23. }

Categories: 3 letnik, C, Fax, SPO Tags:

V C-ju napišite program, ki vsakih 5 sekund izpiše vse procese, ki tečejo na sistemu.

January 9th, 2008 karlosp No comments
CODE:
  1. V C-ju napišite program, ki vsakih 5 sekund izpiše vse procese, ki tečejo na sistemu. V programu ne smete uporabljati funkcij system(...) in popen(...).

C:
  1. #include <stdio .h>
  2. #include <unistd .h>
  3. #include <sys /types.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.    int pid;
  8.        while (1==1)
  9.    {
  10.  
  11.       pid = fork();
  12.       if (pid == -1)
  13.       {
  14.               printf("\nNapaka pri zagonu novega procesa!");   
  15.       }   
  16.       if (pid == 0)
  17.       {
  18.             execlp("ps", argv[0], "-e", 0);
  19.  
  20.       }
  21.       else{
  22.          waitpid (pid, NULL, 0);
  23.     }
  24.  
  25.            sleep(5);
  26.  
  27.    }
  28.    return 0;
  29. }

Categories: 3 letnik, C, Fax, SPO Tags:
71258 pages viewed, 274 today
38330 visits, 133 today
FireStats icon Powered by FireStats