Affichage des articles dont le libellé est insert. Afficher tous les articles
Affichage des articles dont le libellé est insert. Afficher tous les articles

Java Insert JTable Rows Data Into MySQL

How To Insert All JTable Rows Data To MySQL DataBase Using Java NetBeans

Insert JTable Values Into DataBase Using Java



In this Java Tutorial we will see How To INSERT All JTable Rows Values Into DataBase Using For Loop And addBatch Function On Button Click In Java NetBeans .



Project Source Code:


// function to get the connection
    public Connection getConnection()
    {
         try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
             System.out.println(ex.getMessage());
        }
        
        Connection con = null;
        
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/s_t_d", "root", "");
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return con;
    } 


// insert button
    private void jButtonInsertActionPerformed(java.awt.event.ActionEvent evt) {                                              
        
        Connection con = getConnection();
        Statement st;
        
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        
        try {
            st = con.createStatement();
            
            for(int i = 0; i < model.getRowCount(); i++){
                
                int id = Integer.valueOf(model.getValueAt(i, 0).toString());
                String fn = model.getValueAt(i, 1).toString();
                String adr = model.getValueAt(i, 2).toString();
                String bdate = model.getValueAt(i, 3).toString();
                
                String sqlQuery = "INSERT INTO `student`(`Id`, `FullName`, `Address`, `BirthDate`) VALUES ("+id+",'"+fn+"','"+adr+"','"+bdate+"')";
                
                st.addBatch(sqlQuery);
            }
            
            int[] rowsInserted = st.executeBatch();
            System.out.println("Data Inserted");
            System.out.println("rowsInserted Count = " + rowsInserted.length);
            
        } catch (SQLException ex) {
            Logger.getLogger(Insert_All_JTable_Row_Into_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

OutPut:

java add jtable data to mysql




VB.Net Create Menu Using MySQL

How To Make A Menu Using MenuStrip And  MySQL Database In VbNet

vb.net create memu form database

In This VB.Net Tutorial  We Will See How To Create A Menu Using MenuStrip And Add To This Menu Categories Using ToolStripMenuItem With Image And Text From MySQL Database Table And Add Also To This ToolStripMenuItem SubCategorys Elements From MySQL Database Using MySqlDataAdapter + DataTable + MemoryStream In Visual Basic.Net  Programming Language And Visual Studio Editor.


Project Source Code:

Imports MySql.Data.MySqlClient
Imports System.IO

Public Class Create_Menu8from_MySQL

    Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=s_t_d")

  ' add ToolStripMenuItem elements to menustrip using for loop 
    Private Sub Create_Menu8from_MySQL_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim adapter As New MySqlDataAdapter("SELECT * FROM `mypics`", connection)
        Dim table As New DataTable()

        adapter.Fill(table)

        For i As Integer = 0 To table.Rows.Count - 1 Step +1

            Dim img() As Byte
            img = table(i)(3)

            Dim ms As New MemoryStream(img)

            Dim pic As Image = Image.FromStream(ms)

            Dim tsm As New ToolStripMenuItem(table(i)(1), pic)

            addItems(tsm, table(i)(0))

            MenuStrip1.Items.Add(tsm)

        Next

    End Sub

   ' create a function to add subElements to ToolStripMenuItem 
    Sub addItems(ByVal tmenu As ToolStripMenuItem, ByVal id As String)

        Dim adapter As New MySqlDataAdapter("SELECT `idMyPics`, `name`, `pic` FROM `pics2` WHERE `idMyPics` = " + id, connection)
        Dim table As New DataTable()

        adapter.Fill(table)

        For i As Integer = 0 To table.Rows.Count - 1 Step +1

            Dim img() As Byte
            img = table(i)(2)

            Dim ms As New MemoryStream(img)

            Dim pic As Image = Image.FromStream(ms)

            tmenu.DropDown.Items.Add(table(i)(1), pic)

        Next

    End Sub

End Class
      

///////////////OUTPUT:

create menu using mysql database in visual basic.net




VB.Net Add Item To Menu From MySQL

How To Add A New Item To MenuStrip From MySQL Database In VbNet

VB.Net Add New Item To MenuStrip From DataBase

In This VB.Net Tutorial  We Will See How To Create A New ToolStripMenuItem With Image And Text From MySQL Database Table And Add This ToolStripMenuItem To A MenuStrip Element Using MySqlDataAdapter + DataTable + MemoryStream In Visual Basic.Net  Programming Language And Visual Studio Editor.


Project Source Code:

Imports MySql.Data.MySqlClient
Imports System.IO

Public Class Add_Item_To_MenuStrip_From_MySQL

    Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=s_t_d")

    Private Sub Add_Item_To_MenuStrip_From_MySQL_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim adapter As New MySqlDataAdapter("SELECT * FROM `mypics`", connection)
        Dim table As New DataTable()

        adapter.Fill(table)

        For i As Integer = 0 To table.Rows.Count - 1 Step +1

            Dim img() As Byte

            img = table(i)(3)

            Dim ms As New MemoryStream(img)

            Dim pic As Image = Image.FromStream(ms)

            MenuStrip1.Items.Add(table(i)(1), pic)

        Next

    End Sub
End Class
      

///////////////OUTPUT:

Insert ToolStripMenuItem To MenuStrip From DataBase Using VB.Net




VB.Net Insert All DataGridView Data In MySQL

How To Add All DataGridView Values Into MySQL Database Using VbNet

VB.Net Insert All DataGridView Rows In Database

In This VB.Net Tutorial  We Will See How To Populate A Datagridview From Datatable And Add All Datagridview Row's Records In MySQL Database Using For Loop And Mysqlcommand with Parameters In Visual Basic.Net  Programming Language And Visual Studio Editor.


Project Source Code:

Imports MySql.Data.MySqlClient

Public Class Insert_All_DGV_Data_To_MySQL

    Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=s_t_d")

    Private Sub Insert_All_DGV_Data_To_MySQL_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' populate datagridview from datatable 
        Dim table As New DataTable("Table")

        ' Add columns to your datatable 
        table.Columns.Add("Id", Type.GetType("System.Int32"))
        table.Columns.Add("First Name", Type.GetType("System.String"))
        table.Columns.Add("Last Name", Type.GetType("System.String"))
        table.Columns.Add("Age", Type.GetType("System.Int32"))

        ' Add rows to the datatable 
        table.Rows.Add(1, "aaaa", "bbbb", 21)
        table.Rows.Add(2, "SSDD", "hGSQ", 33)
        table.Rows.Add(3, "fgfgd", "jgfdd", 53)
        table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
        table.Rows.Add(5, "hghfd", "ghjgdf", 36)
        table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)

        DataGridView1.DataSource = table

    End Sub

    Private Sub ButtonInsert_Click(sender As Object, e As EventArgs) Handles ButtonInsert.Click

        Dim cmd As MySqlCommand

        connection.Open()

        For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1

            cmd = New MySqlCommand("INSERT INTO `dgv_data`(`id`, `first_name`, `last_name`, `age`) VALUES (@ID, @FN, @LN, @AGE)", connection)

            cmd.Parameters.Add("@ID", MySqlDbType.Int64).Value = DataGridView1.Rows(i).Cells(0).Value.ToString()
            cmd.Parameters.Add("@FN", MySqlDbType.VarChar).Value = DataGridView1.Rows(i).Cells(1).Value.ToString()
            cmd.Parameters.Add("@LN", MySqlDbType.VarChar).Value = DataGridView1.Rows(i).Cells(2).Value.ToString()
            cmd.Parameters.Add("@AGE", MySqlDbType.Int64).Value = DataGridView1.Rows(i).Cells(3).Value.ToString()

            cmd.ExecuteNonQuery()

        Next

        connection.Close()

        MessageBox.Show("All Data Inserted")

    End Sub
End Class
      

///////////////OUTPUT:

VB.Net Insert All DataGridView Records In Database




VB.Net Insert Image Into MySQL

How To Insert Image Into MySQL Database Using VbNet

insert picture in mysql in vbnet

In This VB.Net Tutorial  We Will See How To Browse Picture On Button Click And Display The Selected Picture Into A PictureBox And Insert The Image From PictureBox Into MySQL DataBase Table Using MySqlCommand With Parameters In Visual Basic.Net  Programming Language And Visual Studio Editor.


Project Source Code:

Imports MySql.Data.MySqlClient
Imports System.IO

Public Class Insert_Image_Into_MySQL

    Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=s_t_d")

    ' browse and display image button 
    Private Sub ButtonSelect_Img_Click(sender As Object, e As EventArgs) Handles ButtonSelect_Img.Click

        Dim opf As New OpenFileDialog

        opf.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"

        If opf.ShowDialog = Windows.Forms.DialogResult.OK Then

            PictureBox1.Image = Image.FromFile(opf.FileName)

        End If

    End Sub

    ' insert image button 
    Private Sub ButtonInsert_Click(sender As Object, e As EventArgs) Handles ButtonInsert.Click

        Dim ms As New MemoryStream
        PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)

        Dim command As New MySqlCommand("INSERT INTO `mypics`(`name`, `dscp`, `pic`) VALUES (@nm,@ds,@img)", connection)

        command.Parameters.Add("@nm", MySqlDbType.VarChar).Value = TextBoxName.Text
        command.Parameters.Add("@ds", MySqlDbType.VarChar).Value = TextBoxDesc.Text
        command.Parameters.Add("@img", MySqlDbType.Blob).Value = ms.ToArray()

        connection.Open()

        If command.ExecuteNonQuery() = 1 Then
            MessageBox.Show("Image Inserted")

        Else
            MessageBox.Show("Image Not Inserted")
        End If

        connection.Close()

    End Sub

    Private Sub Insert_Image_Into_MySQL_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class
      
///////////////OUTPUT:

mysql insert picture to database table using visual basic.net




VB.Net And MySQL DataBase - INSERT UPDATE DELETE SEARCH

VB.NET - How To Insert Update Search Delete Data From MySQL Using Visual Basic.Net

vb.net and mysql add edit remove find

In This VB.Net Tutorial  We Will See How To: 
 - Insert Data Into MySQL Database Table.
 - Update Data From MySQL Database Table With A Specific ID.
 - Delete Data From MySQL Database Table With A Specific ID.
 - Search Data In MySQL Database With A Specific ID And Display The Information Into TextBoxes And DateTimePicker ( if data exists )
Using Visual Basic.Net  Programming Language And Visual Studio Editor.


Part 1


Part 2


Project Source Code:

Imports MySql.Data.MySqlClient

Public Class Insert_Update_Delete_Search

    Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=s_t_d")

    ' button find 
    Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click

        Dim search_command As New MySqlCommand("SELECT * FROM `student` WHERE `Id` = @id", connection)

        search_command.Parameters.Add("@id", MySqlDbType.Int64).Value = TextBox1.Text

        Dim adapter As New MySqlDataAdapter(search_command)

        Dim table As New DataTable()

        Try

            adapter.Fill(table)

            If table.Rows.Count > 0 Then

                TextBox2.Text = table(0)(1)
                TextBox3.Text = table(0)(2)
                DateTimePicker1.Value = table(0)(3)

            Else

                TextBox2.Text = ""
                TextBox3.Text = ""
                DateTimePicker1.Value = Now()
                MessageBox.Show("No Data Found")

            End If

        Catch ex As Exception

            MessageBox.Show("ERROR")

        End Try

    End Sub

      ' function to execute the insert update delete commands 
    Function execCommand(ByVal cmd As MySqlCommand) As Boolean

        If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If

        Try
            If cmd.ExecuteNonQuery() = 1 Then
                Return True

            Else
                Return False
            End If
        Catch ex As Exception

            MessageBox.Show("ERROR")
            Return False

        End Try

        If connection.State = ConnectionState.Open Then
            connection.Close()
        End If

    End Function

    ' button add 
    Private Sub ButtonInsert_Click(sender As Object, e As EventArgs) Handles ButtonInsert.Click

        Dim insert_command As New MySqlCommand("INSERT INTO `student`(`FullName`, `Address`, `BirthDate`) VALUES (@fln,@adds,@brd)", connection)
        insert_command.Parameters.Add("@fln", MySqlDbType.VarChar).Value = TextBox2.Text
        insert_command.Parameters.Add("@adds", MySqlDbType.VarChar).Value = TextBox3.Text
        insert_command.Parameters.Add("@brd", MySqlDbType.Date).Value = DateTimePicker1.Value

        If execCommand(insert_command) Then
            MessageBox.Show("Data Inserted")

        Else
            MessageBox.Show("Data NOT Inserted")
        End If

    End Sub

    ' button edit 
    Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click

        Dim update_command As New MySqlCommand("UPDATE `student` SET `FullName`=@fln,`Address`=@adds,`BirthDate`=@brd WHERE `Id` = @id", connection)
        update_command.Parameters.Add("@id", MySqlDbType.Int64).Value = TextBox1.Text
        update_command.Parameters.Add("@fln", MySqlDbType.VarChar).Value = TextBox2.Text
        update_command.Parameters.Add("@adds", MySqlDbType.VarChar).Value = TextBox3.Text
        update_command.Parameters.Add("@brd", MySqlDbType.Date).Value = DateTimePicker1.Value

        If execCommand(update_command) Then
            MessageBox.Show("Data Updated")

        Else
            MessageBox.Show("Data NOT Updated")
        End If

    End Sub

    ' button remove 
    Private Sub ButtonDelete_Click(sender As Object, e As EventArgs) Handles ButtonDelete.Click

        Dim delete_command As New MySqlCommand("DELETE FROM `student` WHERE `Id` = @id", connection)
        delete_command.Parameters.Add("@id", MySqlDbType.Int64).Value = TextBox1.Text

        If execCommand(delete_command) Then
            MessageBox.Show("Data Deleted")

        Else
            MessageBox.Show("Data NOT Deleted")
        End If

    End Sub
End Class

///////////////OUTPUT:

Add Edit Find Remove Data From MySQL Database In Visual Basic.Net