Visit Map

Sunday, January 21, 2007

Java Bean Generator from Database table

Hi again,
I have written a small utility to generate the java bean classes from exiting tables whihc are present in the database.
You can gerenarate your bean class/Model classes with a click of button.
Hope this helps you

Create a from in VB and keep one text boxes and list box places two command buttons
Here is code

Dim conn As ADODB.Connection
Private Sub CommandButton1_Click()Set conn = New ADODB.Connectionconn.Open "xxxx", "xxxx", "xxxx"Dim arr As ADODB.Recordsetconn.CursorLocation = adUseClientSet arr = New ADODB.Recordsetarr.Open "select TNAME from tab", conn, adOpenDynamic, adLockOptimisticDo While Not arr.EOF ListBox1.AddItem (arr.Fields(0)) arr.MoveNextLooparr.Close
End Sub
Private Sub CommandButton2_Click()Dim arr As ADODB.RecordsetDim strValue As StringDim strClassName As StringIf (conn Is Nothing) Then MsgBox "get the tables first", vbCritical Exit SubEnd IfSet arr = New ADODB.RecordsetstrClassName = Replace(ListBox1.Text, "_", "")strValue = "public class " & StrConv(strClassName, vbProperCase) & "{" & vbCrLf
arr.Open "select * " & "from " & ListBox1.Text, conn, adOpenDynamic, adLockOptimisticFor i = 0 To arr.Fields.Count - 1 Select Case arr.Fields(i).Type Case adNumeric, adInteger, adSingle, 139 strValue = strValue + "private int " & Replace(StrConv(arr.Fields(i).Name, vbLowerCase), "_", "") & ";" + Chr(13) Case adVarChar strValue = strValue + "private String " & StrConv(Replace(arr.Fields(i).Name, "_", ""), vbLowerCase) + ";" + Chr(13) End SelectNextstrValue = strValue & "}"TextBox1.Text = strValuearr.Close
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Terminate()If Not (conn Is Nothing) Thenconn.CloseEnd IfEnd Sub
Raj