2016年4月15日 星期五

eclipse連接到mysql

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();

  }
}




4/15

安裝android 在 eclipse <------------------按照這個超連結做

軟體方面:你需要下載3樣東西
1. JAVA開發工具(Java Development kit - JDK)
2. Eclipse的JAVA開發環境(Eclipse IDE for Java Developers)
3. Android 開發工具(stand-alone Android SDK)
※ 各程式下載最新版即可
<a href="http://tccnchsu.blogspot.tw/">d0250356吳昆翰</a>






2016年2月18日 星期四

java SQL & >cd javac **.java javac **


public class HelloWorld { public static void main(String[] args) { System.out.println("Hello! World!"); } } ___________________________________________________ package text1; import java.sql.Connection; import java.sql.DriverManager; public class text1 { 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 WindowsFormsApplication3
{
    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\Desktop\WindowsFormsApplication7\WindowsFormsApplication7\bin\Debug\test1.mdb");
        }

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

            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 button2_Click(object sender, EventArgs e)
        {

        }

        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 tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        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["name"].Value;

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

                    string query = "select* from person where name='" + 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["name"].Value;

                        connection.Open();
                        OleDbCommand command = new OleDbCommand();
                        command.Connection = connection;
                        command.CommandText = "delete from person WHERE name = '" + Value.ToString() + "'";

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

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

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

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

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




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

            command.ExecuteNonQuery();

            dAdapter.Fill(dTable);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            connection.Open();

            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 button2_Click_2(object sender, EventArgs e)
        {
                 dTable.Clear();

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




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

                      command.ExecuteNonQuery();

                      dAdapter.Fill(dTable);
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}

       
   

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.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int com = 0;
        Random rnd = new Random();
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.ReadOnly = true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            com = rnd.Next(1, 4);
            if (com == 1)
                textBox1.Text = "電腦出剪刀,平手";
            else if (com == 2)
                textBox1.Text = " 電腦出石頭,lose";
            else if (com == 3)
                textBox1.Text = "電腦出布,win";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            com = rnd.Next(1, 4);
            if (com == 1)
                textBox1.Text = "電腦出剪刀,win";
            else if (com == 2)
                textBox1.Text = " 電腦出石頭,平手";
            else if (com == 3)
                textBox1.Text = "電腦出布,lose";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            com = rnd.Next(1, 4);
            if (com == 1)
                textBox1.Text = "電腦出剪刀,lose";
            else if (com == 2)
                textBox1.Text = " 電腦出石頭,win";
            else if (com == 3)
                textBox1.Text = "電腦出布,平手";
        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

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];
        public Form1()
        {
            InitializeComponent();
            textBox1.DataBindings.Add("Text", bindingSource3, "name");
            textBox2.DataBindings.Add("Text", bindingSource3, "math");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 這行程式碼會將資料載入 'db1DataSet.DataTable1' 資料表。您可以視需要進行移動或移除。
            this.dataTable1TableAdapter.Fill(this.db1DataSet.DataTable1);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.tb1' 資料表。您可以視需要進行移動或移除。
            this.tb1TableAdapter.Fill(this.db1DataSet.tb1);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.record' 資料表。您可以視需要進行移動或移除。
            this.recordTableAdapter.Fill(this.db1DataSet.record);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.person' 資料表。您可以視需要進行移動或移除。
            this.personTableAdapter.Fill(this.db1DataSet.person);

        }

        private void bindingNavigator2_RefreshItems(object sender, EventArgs e)
        {

        }

        private void tabPage4_Click(object sender, EventArgs e)
        {

        }
    }
}

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
   


        }

        private void button3_Click(object sender, EventArgs e)
        {
            Double a = Double.Parse(textBox1.Text);
            Double b = Double.Parse(textBox2.Text);
            double c = a * b;
            label2.Text = c.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Double a = Double.Parse(textBox1.Text);
            Double b = Double.Parse(textBox2.Text);
            double c = a + b;
            label2.Text = c.ToString();


           
       

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Double a = Double.Parse(textBox1.Text);
            Double b = Double.Parse(textBox2.Text);
            double c = a - b;
            label2.Text = c.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Double a = Double.Parse(textBox1.Text);
            Double b = Double.Parse(textBox2.Text);
            double c = a / b;
            label2.Text = c.ToString();
        }
    }
}