2016年2月18日 星期四

打開JAVA

打開JAVA

import java.sql.Connection;
import java.sql.DriverManager;
public class hyt {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection c = null;
  try {
   Class.forName("org.sqlite.JDBC");
   c = DriverManager.getConnection("jdbc:sqlite:test.db");
  } catch (Exception e) {
   System.err.println(e.getClass().getName() + ": " + e.getMessage());
   System.exit(0);
  }
  System.out.println("Opened database successfully");
 }
}


2015年12月25日 星期五

資料庫期末考

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.OleDb;//**
using System.IO;//**

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        private OleDbConnection connection = new OleDbConnection();

        OleDbDataAdapter dAdapter;
        OleDbCommandBuilder cBuilder;
        DataTable dTable = new DataTable();
        BindingSource bSource;

        private string ID;

        public Form1()
        {
           
            InitializeComponent();
            connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\student\Downloads\WindowsFormsApplication7\WindowsFormsApplication7\bin\Debug\test1.mdb");


        }

   

        private void Form1_Load(object sender, EventArgs e)
        {
       

            dAdapter = new OleDbDataAdapter("select * from person where 識別碼  ", connection);

            cBuilder = new OleDbCommandBuilder(dAdapter);
            dAdapter.Fill(dTable);

            bSource = new BindingSource();
            bSource.DataSource = dTable;

            dataGridView1.DataSource = bSource;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            connection.Open();
            dTable.Clear();

            OleDbCommand command2 = new OleDbCommand();
            command2.Connection = connection;

            command2.CommandText = "insert into person (stu_no,name,sex,tel) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";

            command2.ExecuteNonQuery();

            dAdapter.Fill(dTable);

            connection.Close();
             
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {

                try
                {
                    var Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                    Value = dataGridView1.Rows[e.RowIndex].Cells["stu_no"].Value;

                    connection.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = connection;

                    string query = "select* from person where stu_no='" + Value.ToString() + "'";
                    command.CommandText = query;


                    OleDbDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {

                        ID = reader["識別碼"].ToString();
                        textBox1.Text = reader["stu_no"].ToString();
                        textBox2.Text = reader["name"].ToString();
                        textBox3.Text = reader["sex"].ToString();
                        textBox4.Text = reader["tel"].ToString();
                       

                    }
                    connection.Close();
                }

                catch (Exception ex)
                {
                    MessageBox.Show("ERROR" + ex);
                }
            }

            else if (e.ColumnIndex == 1)
            {
                if (MessageBox.Show("確定刪除此筆資料?", "刪除資料", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        var Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                        Value = dataGridView1.Rows[e.RowIndex].Cells["stu_no"].Value;
                       
                        connection.Open();
                        OleDbCommand command = new OleDbCommand();
                        command.Connection = connection;

               

                        command.CommandText = "delete from person WHERE stu_no = '" + Value.ToString() + "'";

                        command.ExecuteNonQuery();
                        dTable.Clear();
                        dAdapter.Fill(dTable);

                        connection.Close();
                        MessageBox.Show("刪除成功");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("ERROR" + ex);
                    }

                }
            }




        }

        private void button2_Click(object sender, EventArgs e)
        {
                         dTable.Clear();

                        connection.Open();
                        OleDbCommand command = new OleDbCommand();
                        command.Connection = connection;




                        command.CommandText = "UPDATE person SET stu_no = '" + textBox1.Text + "'WHERE 識別碼 = " + ID ;

                      command.ExecuteNonQuery();

                      dAdapter.Fill(dTable);
        }
    }
}

2015年12月3日 星期四

資料庫移動

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { // TODO: 這行程式碼會將資料載入 'db1DataSet.DataTable1' 資料表。您可以視需要進行移動或移除。 this.dataTable1TableAdapter.Fill(this.db1DataSet.DataTable1); // TODO: 這行程式碼會將資料載入 'db1DataSet.record' 資料表。您可以視需要進行移動或移除。 this.recordTableAdapter.Fill(this.db1DataSet.record); // TODO: 這行程式碼會將資料載入 'db1DataSet.person' 資料表。您可以視需要進行移動或移除。 this.personTableAdapter.Fill(this.db1DataSet.person);
}
private void tabPage2_Click(object sender, EventArgs e) {
}
private void textBox1_TextChanged(object sender, EventArgs e) {
}
private void button1_Click(object sender, EventArgs e) { // If you are not at the end of the list, move to the next item // in the BindingSource. if (bindingSource3.Position + 1 < bindingSource3.Count) bindingSource3.MoveNext();
// Otherwise, move back to the first item. else bindingSource3.MoveFirst(); // Force the form to repaint. this.Invalidate(); }
private void button2_Click(object sender, EventArgs e) { // If you are not at the end of the list, move to the next item // in the BindingSource. if (bindingSource3.Position + 1 < bindingSource3.Count) bindingSource3.MovePrevious();
// Otherwise, move back to the first item. else bindingSource3.MoveLast(); // Force the form to repaint. this.Invalidate(); } private void button3_Click(object sender, EventArgs e) {
textBox1.DataBindings.Add("Text", bindingSource3, "chinese"); } } }

2015年11月20日 星期五

資料庫

資料庫

將打好的表單貯存為.mdb

使用bindingSource連上表單

用dataGridView將表單內容顯示出來



2015年11月5日 星期四

計算機


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        String aa;
        String m;

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            textBox1.Text += b.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            Button mb = sender as Button;
            m = mb.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            Button mb = sender as Button;
            m = mb.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            Button mb = sender as Button;
            m = mb.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            Button mb = sender as Button;
            m = mb.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            {
                Double a = Double.Parse(aa);
                Double b = Int32.Parse(textBox1.Text);
                Double c = 0;

                switch (m)
                {
                    case "+":
                        c = a + b;
                        break;
                    case "-":
                        c = a - b;
                        break;
                    case "*":
                        c = a * b;
                        break;
                    case "/":
                        c = a / b;
                        break;

                }
                textBox1.Text = c.ToString();
            }

        }

        private void button16_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void button17_Click(object sender, EventArgs e)
        {

        }
    }
}

2015年10月30日 星期五

陣列按鈕




基本陣列按鈕

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Button[,] Buttons = new System.Windows.Forms.Button[5, 5];
        int r = 0, r6 = 0;
        public Form1()

        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Buttons[i, j] = new Button();
                    Buttons[i, j].Size = new Size(50, 50);
                    Buttons[i, j].Location = new Point(i * 50, j * 50);
                    this.Controls.Add(Buttons[i, j]);//出現在畫面中
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random r2 = new Random();
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    r6 = i + j * 4;
                    r = r2.Next(0, 15);
                    Buttons[i, j].Text = r6.ToString();
                }
            }
        }
    }
}


產生隨機亂數陣列按鈕

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Button[,] Buttons = new System.Windows.Forms.Button[5, 5];
        int r = 0, r6 = 0;
        public Form1()

        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Buttons[i, j] = new Button();
                    Buttons[i, j].Size = new Size(50, 50);
                    Buttons[i, j].Location = new Point(i * 50, j * 50);
                    this.Controls.Add(Buttons[i, j]);//出現在畫面中
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random r2 = new Random();
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    r6 = i + j * 4;
                    r = r2.Next(0, 15);
                    Buttons[i, j].Text = r.ToString();
                }
            }
        }
    }
}