2016年4月15日 星期五

eclipse連接到mysql

http://blog.yslifes.com/archives/918    <-這個網址 按照步驟做

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class jdbcmysql {
  private Connection con = null; //Database objects
  //連接object
  private Statement stat = null;
  //執行,傳入之sql為完整字串
  private ResultSet rs = null;
  //結果集
  private PreparedStatement pst = null;
  //執行,傳入之sql為預儲之字申,需要傳入變數之位置
  //先利用?來做標示

  private String dropdbSQL = "DROP TABLE User ";

  private String createdbSQL = "CREATE TABLE User (" +
    "    id     INTEGER " +
    "  , name    VARCHAR(20) " +
    "  , passwd  VARCHAR(20))";

  private String insertdbSQL = "insert into User(id,name,passwd) " +
      "select ifNULL(max(id),0)+1,?,? FROM User";

  private String selectSQL = "select * from User ";

  public jdbcmysql()
  {
    try {
      Class.forName("com.mysql.jdbc.Driver");
      //註冊driver
      con = DriverManager.getConnection(
      "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5",
      "","");
      //取得connection

//jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5
//localhost是主機名,test是database名
//useUnicode=true&characterEncoding=Big5使用的編碼
   
    }
    catch(ClassNotFoundException e)
    {
      System.out.println("DriverClassNotFound :"+e.toString());
    }//有可能會產生sqlexception
    catch(SQLException x) {
      System.out.println("Exception :"+x.toString());
    }
 
  }
  //建立table的方式
  //可以看看Statement的使用方式
  public void createTable()
  {
    try
    {
      stat = con.createStatement();
      stat.executeUpdate(createdbSQL);
    }
    catch(SQLException e)
    {
      System.out.println("CreateDB Exception :" + e.toString());
    }
    finally
    {
      Close();
    }
  }
  //新增資料
  //可以看看PrepareStatement的使用方式
  public void insertTable( String name,String passwd)
  {
    try
    {
      pst = con.prepareStatement(insertdbSQL);
   
      pst.setString(1, name);
      pst.setString(2, passwd);
      pst.executeUpdate();
    }
    catch(SQLException e)
    {
      System.out.println("InsertDB Exception :" + e.toString());
    }
    finally
    {
      Close();
    }
  }
  //刪除Table,
  //跟建立table很像
  public void dropTable()
  {
    try
    {
      stat = con.createStatement();
      stat.executeUpdate(dropdbSQL);
    }
    catch(SQLException e)
    {
      System.out.println("DropDB Exception :" + e.toString());
    }
    finally
    {
      Close();
    }
  }
  //查詢資料
  //可以看看回傳結果集及取得資料方式
  public void SelectTable()
  {
    try
    {
      stat = con.createStatement();
      rs = stat.executeQuery(selectSQL);
      System.out.println("ID\t\tName\t\tPASSWORD");
      while(rs.next())
      {
        System.out.println(rs.getInt("id")+"\t\t"+
            rs.getString("name")+"\t\t"+rs.getString("passwd"));
      }
    }
    catch(SQLException e)
    {
      System.out.println("DropDB Exception :" + e.toString());
    }
    finally
    {
      Close();
    }
  }
  //完整使用完資料庫後,記得要關閉所有Object
  //否則在等待Timeout時,可能會有Connection poor的狀況
  private void Close()
  {
    try
    {
      if(rs!=null)
      {
        rs.close();
        rs = null;
      }
      if(stat!=null)
      {
        stat.close();
        stat = null;
      }
      if(pst!=null)
      {
        pst.close();
        pst = null;
      }
    }
    catch(SQLException e)
    {
      System.out.println("Close Exception :" + e.toString());
    }
  }


  public static void main(String[] args)
  {
    //測看看是否正常
    jdbcmysql test = new jdbcmysql();
    test.dropTable();
    test.createTable();
    test.insertTable("yku", "12356");
    test.insertTable("yku2", "7890");
    test.SelectTable();

  }
}
先在瀏覽器上搜尋 java jdk  點選第一個 Java SE - Downloads
進入之後點游標所在的Java
再來點選游標所在的Accept License Agreement
之後畫面會變為下圖

接著看自己的電腦屬於哪種的 (游標移到我的電腦 按右鍵 內容就可以知道了) 點選一下就可以下載喽
接著我們用一個簡單的例子試試看
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello! World!");
    }
}
將上面程式碼複製到桌面新增文件 檔名為“HelloWorld.java”
成功跑出HelloWorld喽~~

第一支Android app程式教學




new11.jpg



軟體方面:你需要下載3樣東西
1. JAVA開發工具(Java Development kit - JDK)
2. Eclipse的JAVA開發環境(Eclipse IDE for Java Developers)
3. Android 開發工具(stand-alone Android SDK)
※ 各程式下載最新版即可
※ 有時候安裝不成功,不是你的問題,是程式的問題,請等官方解決



以上就是開發工具的安裝和開啟專案的步驟
至於Android APP要怎麼寫:

1. 首先,要會一些基礎JAVA

2. 去下面網址,從「建立第一個程式」看起,往下看完就學得差不多了
https://code.google.com/p/androidbmi/wiki/AndroidUI




2015年12月17日 星期四


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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.DataBindings.Add("Text", bindingSource1, "name", true);
            textBox2.DataBindings.Add("Text", bindingSource1, "sex", true);
            textBox3.DataBindings.Add("Text", bindingSource1, "tel", true);
            textBox4.DataBindings.Add("Text", bindingSource1, "stu_no", true);
        }

        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 button1_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveFirst();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveNext();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bindingSource1.MovePrevious();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSource1.AddNew();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSource1.EndEdit();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

}





tabcontrol - tabpage
binding Source
datagridview
bindingNavigator - biding source

textbox
label

2015年12月11日 星期五

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
    {
        int[] array = new int[4];
        int mysum = 0;
        public Form1()
        {
            InitializeComponent();
            textBox1.DataBindings.Add("Text", bindingSource3, "name", true);
            textBox2.DataBindings.Add("Text", bindingSource3, "math", true);
            array[0] = 0;
            array[1] = 0;
            array[2] = 0;
            array[3] = 0;
        }

        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 dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void bindingNavigatorMoveNextItem2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            bindingSource3.MoveFirst();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bindingSource3.MoveNext();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            array[0] = int.Parse(textBox2.Text);
            for (int i = 0; i < 3; i++)
            {
                array[i] = int.Parse(textBox2.Text);
                bindingSource3.MoveNext();
            }
            for (int i = 0; i < 3; i++)
            {
                array[i] = mysum + array[i];
            }
            textBox3.Text = mysum.ToString();
        }
    }
}


tabcontrol - tabpage
binding Source
datagridview
bindingNavigator - biding source

textbox
label


2015年12月3日 星期四

C# 資料庫




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: 這行程式碼會將資料載入 'db1DataSet1.DataTable2' 資料表。您可以視需要進行移動或移除。
            this.dataTable2TableAdapter.Fill(this.db1DataSet1.DataTable2);
            // 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 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 (bindingSource1.Position + 1 < bindingSource1.Count)
                bindingSource1.MoveNext();

            // Otherwise, move back to the first item.
            else
                bindingSource1.MoveFirst();


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            // Force the form to repaint.
            //this.Invalidate();
            //textBox1.DataBindings.Add("Text", bindingSource1, "name");
        }

        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 (bindingSource1.Position - 1 > bindingSource1.Count)
                bindingSource1.MoveNext();

            // Otherwise, move back to the first item.
            else
                bindingSource1.MoveFirst();


        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Force the form to repaint.
            this.Invalidate();
            textBox1.DataBindings.Add("Text", bindingSource1, "name");
        }


    }
}


binding Source - dataset
tabcontrol
datagridview
bindingNavigator
textbox