Tips&Tricks | I trucchi del mestiere |
![]() |
Ottimizzare lutilizzo dei combobox |
Tabella_Persone ID Contatore Chiave primaria Cognome Testo (20) Indicizzato (duplicati ammessi) Nome Testo (20) Indicizzato (duplicati ammessi) Data_di_nascita Data/Ora Luogo_di_nascita Testo (40) Codice_fiscale Testo (16) Indicizzato (duplicati non ammessi) Attivo Sì/No |
![]() |
Copiare i dati tra due database |
import java.sql.*; public class SQLServerToAccess { static final String FORNAME="sun.jdbc.odbc.JdbcOdbcDriver"; static String DriverInput; static String DriverOutput; public static void main(String[] args) throws ClassNotFoundException{ try{ Class.forName(FORNAME); DriverInput="jdbc:odbc:"+args[0]; //driver del database sql DriverOutput="jdbc:odbc:"+args[1]; //driver del database access Connection conIn=DriverManager.getConnection(DriverInput); Connection conOut=DriverManager.getConnection(DriverOutput); Statement stIn=conIn.createStatement(ResultSet.TYPE_SCROLL_INSE NSITIVE,ResultSet.CONCUR_READ_ONLY); ResultSet rs=stIn.executeQuery("select * from Utenti");//il resultset dev'essere scrollabile a causa di un problema rs.first(); do{ String nomeUtente=rs.getString(1); String cognomeUtente=rs.getString(2); int codiceUtente=rs.getInt(3); Statement stOut=conOut.createStatement(); stOut.execute("INSERT INTO Utenti VALUES('"+nomeUtente+"','"+cognomeUtente+"',"+ codiceUtente+")"); } while(rs.next()); rs.last(); //questa e le righe a seguire servono ad ovviare ad un problema di inserimento dell'ultimo valore Statement first=conOut.createStatement(); //inserito nel DB di partenza first.execute("insert into Utenti values('"+rs.getString(1)+"','"+rs.getString(2)+"',"+rs.getI nt(3)+")"); System.out.println("Copia Terminata!"); System.exit(0); }catch(SQLException e){ System.out.println("SQL Exception!"); System.exit(1); } } } |
![]() |
Eseguire operazioni in background |
import java.util.*; public class BackgroundJobExecutor implements Runnable { private Queue queue; private boolean stopRequest = false; private boolean stopping = false; private Thread thread; private final AbstractJob nop = new AbstractJob() { public void execute() { dispose(); } }; public BackgroundJobExecutor() { queue = new Queue(); thread = new Thread(this); } /** * Inizia a processare i job */ public void start() { thread.start(); } /** * Aggiunge un job alla coda */ public boolean add(AbstractJob job) { if (job == null) { throw new NullPointerException("Job cannot be null"); } if (stopRequest || stopping) { throw new IllegalArgumentException("BackgroundJobExecutor has been stopped."); } return queue.put(job); } public void run() { while (!stopRequest) { AbstractJob l = (AbstractJob)queue.get(); if (l == null) { break; } l.execute(); } dispose(); } /** * Termina i job in coda e si ferma */ public void stop() { if (stopRequest) { throw new IllegalArgumentException("BackgroundJobExecutor has been stopped."); } if (!stopping) { stopping = true; queue.putLast(nop); } } /** * Ferma l'esecusione senza necessariamente aver terminati i job in coda */ public void dispose() { stopRequest = true; queue.clear(); if (thread != null) { thread.interrupt(); thread = null; } } private class Queue { private List list = new LinkedList(); public synchronized Object get() { while (list.size() == 0) { try { wait(); } catch (InterruptedException ex) { return null; } } Object o = list.iterator().next(); list.remove(o); return o; } public synchronized boolean put(Object obj) { boolean b = list.add(obj); notify(); return b; } public synchronized void clear() { list.clear(); } public synchronized void putLast(Object o) { list.add(list.size(), o); } } } |
![]() |
Una classe che implementa il controllo e/o la creazione del codice fiscale |
<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> |
![]() |
Utilizzo dei template |
<a href="menu.htm">menu</a> |
<p>$menu</p> |
<? $collegamento = "home.htm"; $voce = "Torna all'homepage"; eval("\$menu .= \"".$vocedimenu."\";"); \\ dove $vocedimenu contiene l'elemento dell'esempio precedente. $collegamento = "links.htm"; $voce = "Visita i link"; eval("\$menu .= \"".$vocedimenu."\";"); eval("\$outputfinale .= \"".$colonnasinistra."\";"); \\ $colonnasinistra contiene il template della ?> |
![]() |
Una rappresentazione grafica per i nostri dati |
![]() |
Come inviare un FAX dal proprio PC |
Option Explicit Private Const OF_EXIST = &H4000 Private Const OFS_MAXPATHNAME = 128 Private Const ERROR_FILE_NOT_FOUND = 2& Private Const HFILE_ERROR = (-1) Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long Private Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(OFS_MAXPATHNAME) As Byte End Type Public Function FileExists(Path As String) As Boolean Dim o As OFSTRUCT Dim i As Integer o.cBytes = Len(o) i = OpenFile(Path, o, OF_EXIST) If (i > HFILE_ERROR) Then FileExists = True Else FileExists = (Err.LastDllError <> ERROR_FILE_NOT_FOUND) End If Err.Clear End Function Public Sub DisabilitaControlli(value As Boolean) Dim c As Control Frame1.Enabled = value For Each c In FormMain.Controls On Error Resume Next If (c.Container.Name = Frame1.Name) Then c.Enabled = value Err.Clear Next End Sub Function ComputerName() As String Dim buffer As String * 512, length As Long length = Len(buffer) If GetComputerName(buffer, length) Then ComputerName = Left$(buffer, length) End If End Function Private Sub CHK_COVER_Click() Dim i As Integer Dim c As Control If (CHK_COVER.value = 1) Then DisabilitaControlli True Else DisabilitaControlli False End If End Sub Private Sub Command1_Click() Dim objFaxServer As Object Dim objFaxDoc As Object On Error GoTo ErrTrap If (RTrim(TXT_SERVER.Text) <= "") Then MsgBox "Specificare il server", vbCritical Or vbOKOnly, Caption TXT_SERVER.SetFocus Exit Sub End If If (RTrim(TXT_FILE.Text) <= "" Or Not FileExists(TXT_FILE.Text)) Then MsgBox "Specificare il nome dell'alegato (Pdf/Word)", vbCritical Or vbOKOnly, Caption TXT_FILE.SetFocus Exit Sub End If If (RTrim(TXT_NUMERO.Text) <= "") Then MsgBox "Specificare il numero telefono", vbCritical Or vbOKOnly, Caption TXT_NUMERO.SetFocus Exit Sub End If If (CHK_COVER.value = 1) Then If (RTrim(TXT_COVER.Text) <= "" Or Not FileExists(TXT_COVER.Text)) Then MsgBox "Specificare una cover", vbCritical Or vbOKOnly, Caption TXT_COVER.SetFocus Exit Sub End If End If Set objFaxServer = CreateObject("FaxServer.FaxServer") objFaxServer.Connect (TXT_SERVER.Text) Set objFaxDoc = objFaxServer.CreateDocument(TXT_FILE.Text) objFaxDoc.FaxNumber = TXT_NUMERO.Text objFaxDoc.RecipientName = TXT_DESTINATARIO.Text If (CHK_COVER.value = 1) Then objFaxDoc.SendCoverPage = 1 objFaxDoc.CoverPageName = TXT_COVER.Text objFaxDoc.CoverPAgeSubject = TXT_OGGETTO.Text objFaxDoc.CoverPageNote = TXT_NOTE.Text End If objFaxDoc.Send Set objFaxDoc = Nothing objFaxServer.Disconnect Set objFaxServer = Nothing Exit Sub ErrTrap: Select Case MsgBox("Errore:(" & Err.Number & ")" & Err.Description, vbAbortRetryIgnore Or vbCritical, Caption) Case vbRetry Resume Case vbAbort If (Not objFaxDoc Is Nothing) Then Set objFaxDoc = Nothing If (Not objFaxServer Is Nothing) Then Set objFaxServer = Nothing Exit Sub Case vbIgnore Resume Next End Select End Sub Private Sub Command3_Click() CommonDialog.Filter = "Documenti Word|*.doc|Acrobat reader|*.pdf" CommonDialog.ShowOpen TXT_FILE.Text = CommonDialog.FileName End Sub Private Sub Command2_Click() CommonDialog.Filter = "Cover Pages|*.cov" CommonDialog.ShowOpen TXT_COVER.Text = CommonDialog.FileName End Sub Private Sub Form_Load() TXT_SERVER.Text = GetSetting(App.EXEName, "INPUT", "SERVER") If (RTrim(TXT_SERVER.Text) <= "") Then TXT_SERVER.Text = ComputerName End If TXT_FILE.Text = GetSetting(App.EXEName, "INPUT", "FILE") TXT_NUMERO.Text = GetSetting(App.EXEName, "INPUT", "NUMERO") CHK_COVER.value = 0 DisabilitaControlli False End Sub Private Sub Form_Unload(Cancel As Integer) SaveSetting App.EXEName, "INPUT", "SERVER", TXT_SERVER.Text SaveSetting App.EXEName, "INPUT", "FILE", TXT_FILE.Text SaveSetting App.EXEName, "INPUT", "NUMERO", TXT_NUMERO.Text End Sub |
![]() |
Manipolare un documento Word da Visual Basic |
|