Powered By Blogger
Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

06 May 2011

05 April 2011

Run Java query MySQL

java File ชื่อ listtable.java

-----------------------------------------------

import java.sql.* ;

public class listtable{
 public static void main(String[] args) throws Exception
 {
  System.out.println("OK");

//  String sql = "show databases;" ;
  String sql = "show tables;" ;
  getdb( sql ) ;

 }
 public static void getdb(String sql ) throws Exception
 {
  Class.forName("com.mysql.jdbc.Driver").newInstance();

  String url = "jdbc:mysql://127.0.0.1:3306/database_name" ;
  String user = "user" ;
  String pass  = "password" ;
  Connection con = DriverManager.getConnection(url , user, pass) ;
  Statement stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery(sql ) ;
  while(rs.next() ){
   System.out.println(rs.getString(1));
  }
  rs.close();
  stmt.close();
  con.close();
 }
}

-------------------------------------------------------------------------
แล้ว สร้าง file run.bat ดังนี้

SET JAVA_HOME=c:\Program Files\Java\jdk1.6.0_13
set classpath=.;c:\Program Files\Java\jdk1.6.0_13\jre\lib\ext\classes12_g.jar;.\mysql-connector-java-5.1.10-bin.jar
javac  listtable.java
pause
java   listtable

-------------------------------------------------------------------------
โดย ใน Folder ที่เก็บ File *.java และ *.bat จะอยู่ที่เดียวกัน
และมีรายการ File ดังนี้


04/05/2011  01:41 PM  

         .
04/05/2011  01:41 PM  
         ..
04/05/2011  01:25 PM               843 listtable.java
09/22/2009  04:01 PM           724,225 mysql-connector-java-5.1.10-bin.jar
04/05/2011  01:25 PM               270 run.bat


-------------------------------------------------------------------------
File mysql-connector-java-5.1.10-bin.jar  เป็น file ที่อยู่ใน
path
c:\Program Files\Java\jdk1.6.0_13\jre\lib\ext\
ที่ได้รับการติดตั้ง พร้อม jdk1.6.0 _13

copy จาก
c:\Program Files\Java\jdk1.6.0_13\jre\lib\ext\mysql-connector-java-5.1.10-bin.jar
มาไว้ที่ folder ที่มี *.java *.bat


หรืออาจจะโหลดจาก web mysql ( oracle )
ที่หัวข้อ  connector/j  
แต่ต้องมี user password ก่อน
http://dev.mysql.com/downloads/connector/j/

---------------------------------------------------------------------------
แล้วรัน
> run.bat  จะ แสดงรายการ table ในฐานข้อมูล mysql นั้น


---------------------------------------------------------------------------












13 October 2010

reset mysql password in windows environment


mysqld-nt --skip-grant-tables --user=root

เปิด อีก cmd.exe

C:\Users\user>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.0.45-community-nt-log MySQL Community Edition (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use mysql
Database changed
mysql> update user set PASSWORD=PASSWORD('aa') where user='root';
Query OK, 0 rows affected (0.00 sec)Rows matched: 1  Changed: 0  Warnings: 0
mysql> flush privileges ;
Query OK, 0 rows affected (0.02 sec)
mysql> quit
Bye

C:\Users\user>mysql -u root -p
Enter password: **
Welcome to the MySQL monitor.  
Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.0.45-community-nt-log MySQL Community Edition (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

Close mysql-nt.exe  ( first window ) by
C:\Users\user>mysqladmin shutdown -u root -p
Enter password: **

C:\Users\user>

10 October 2010

setup apache2 on ubuntu-server 6

************************************************************************
Note by Thitipong samranvanit  
Ubuntu Server , Install with LAMP 
Sun VM VirtualBox , ver 3.2.8 
Date  9 / 10 / 2010   
Document tools : Ubuntu Server + vi  ( 6 )
************************************************************************
I download LiveCD-Server6 from Ubuntu 
and Install In HD ( Oracle VirtualBox )

set ipconfig by  

 # ifconfig eth0 192.168.1.102  netmask 255.255.255.0  up 

set default gateway by 
 # route add default via 192.168.1.1 

In my machin  use  Oracle VirtualBox 

1 NetworkCard is Bridge Mode , Adapter1 as Bridge Adapter , Cable Connected
2 , Adapther 2 as NAT  , Cable not connected

I start ping gateway by ping  192.168.1.1 
and after success , then ping  www.google.co.th

end ping process by  Ctrl + C  (  must  LEFT Ctrl in VirtualBox  )

******************************
Apache2
******************************
Apache2 is in /etc/Apache2
config not in  /etc/apache2/httpd.conf
But  config in /etc/apache2/apache2.conf 

root directory is in 
/var/www 

You must copy file from  /var/www/default-www/*.html.en  to  /var/www by "cp"
and copy *.gif ,*.png from /var/www/default-ww/*.* to /var/www
then rename *.html.en to *.html   by  command "mv"    

then  open browser on host PC ( ex. Windows XP + Chrome ) 
and   enter url  http://192.168.1.102   
then  you will find  Apache page 

start/stop apache2 by
# /etc/init.d/apache2 start 
# /etc/init.d/apache2 stop 
or you can use "sudo" if you not root
$sudo /etc/init.d/apache2 start 
( must enter password of root again ) 


*********************************
To Test PHP 
*********************************
Create 1 file named  phpinfo.php
add this below code


end then  save in  /var/www

open browser in Host File to browse file 
http://192.168.1.102/phpinfo.php

***********************************
Test Mysql
***********************************
mysql is in /etc/mysql  
with config file my.cfg

Can check mysql by run at prompt 
#mysql  {enter}   to run mysql client 
if mysql server running (by default ) 
it will connect to mysql server database
mysql> 

run command to show database by 
mysql>show databases;
will list database 

(now mysql is part of Oracle ) 

***********************************
SYSTEM 
***********************************
hostname can view by command 
#cat /etc/hostname 

installing new software
apt-get install [pacakge] 
this command may tell you unknown .

use aptitude for more information 
#aptitude install [pacakge]
or user 
#aptitude   
to display  GUI (text mode) on Linux 
quit gui by "q"  
user F1  on keyboard for help

for Searching package use "/"
searching Apache2 may use 
"apache"  or "httpd" , in this machin 
found  "apache" 

you will find description of many program
on aptitude 
( most linux program name very unclear for WinXP user)

***********************************
Download File
***********************************
you can download file with 
wget ( see in aptitude ) 

***********************************
NOTE: in this Ubuntu Server (TextMode Only ) 
,there is no other files in /var/www  
***********************************






   


25 November 2008

เปลี่ยน port mysql ใน AppServ

ในที่นี้ เนื่องจาก ติดตั้ง mysql 2 instance บนเครื่องเดียวกัน
และ มีการ ใช้ port ชนกันจึงต้องเปลี่ยน ที่ ตัวหนึ่้ง
( หลังจาก มีการเปลี่ยน port ของ apache web server ไปแล้ว )
โดยให้ instance ตัวหนึ่ง ใช้ port 3306 ปกติ
และอีกตัว ใช้ port 3307

ต้องการเปลี่ยน port ก็ แก้โดย
C:\AppServ\MySQL\my.ini

เปลี่ยน section
[client]
port=3306

ให้กลายเป็น
[client]
port=3307

และต้องเปลี่ยน phpMyAdmin อีกให้ มองเห็นตัวนี้ รวมทั้ง เมื่อจะติดต่อด้วย php
ต้องเรียกด้วย
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
หรือ
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');


แก้ไข ที่ phpMyAdmin ดังนี้
C:\AppServ\www\phpMyAdmin
config.inc.php แก้ไข จาก

cfg['Servers'][$i]['port'] = '';

ให้เป็น
cfg['Servers'][$i]['port'] = '3307';

แล้ว ลอง start mysql ใหม่
และ start phpmyadmin ใหม่ จาก browser

ถ้าทำการเปลี่ยน port ของ apache ต้อง restart apache ใหม่ด้วยครับ

mysql and mysqladmin

ดู help ของ mysql.exe
ได้โดย
>mysql.exe --help
>mysqld-nt.exe --help
>mysqld.exe --help


connect database
mysql.exe --user=myuser --pass=mypassword --host=myhost.com
mysql.exe --user=myuser --pass=mypassword --host=localhost
mysql.exe --user=myuser --pass=mypassword --host=127.0.0.1

connect database at localhost
mysql.exe

query data
>show databases
>use mydatabase
>show tables
>select count(*) from table1

mysql is active status
mysqladmin.exe ping ----- view status of mysql at localhost
mysqladmin --host=192.168.0.10 --user=root --pass=mmppst ping
mysqladmin --host=myhostname --user=root --pass=mmppst ping

show current mysql server variable (with path )
mysqladmin --host=myhostname.com --user=root --pass=mmppst variables
mysqladmin --host=myhostname.com --user=root --pass=mmppst variables more

shutdown mysql server
mysqladmin --host=myhostname.com --user=root --pass=mmppst shutdown

view version
mysqladmin.exe --host=myhostname.com --user=root --pass=mmppst version

19 November 2008

เปลี่ยน port mysql ใน AppServ

ในที่นี้ เนื่องจาก ติดตั้ง mysql 2 instance บนเครื่องเดียวกัน
และ มีการ ใช้ port ชนกันจึงต้องเปลี่ยน ที่ ตัวหนึ่้ง
( หลังจาก มีการเปลี่ยน port ของ apache web server ไปแล้ว )
โดยให้ instance ตัวหนึ่ง ใช้ port 3306 ปกติ
และอีกตัว ใช้ port 3307

ต้องการเปลี่ยน port ก็ แก้โดย
C:\AppServ\MySQL\my.ini

เปลี่ยน section
[client]
port=3306

ให้กลายเป็น
[client]
port=3307

หมายเหตุ ในส่วน [client] อาจไม่ต้องแก้ไขก็ได้ เพราะจะ ใช้กับ mysql.exe ซึ่งเป็น client
แต่ server ส่วนใหญ่จะเป็น 3306 อยู่แล้ว อาจแก้ไขเฉพาะ [mysqld] ก็พอ

และต้องเปลี่ยน section mysqld จาก
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306

ไปเป็น
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3307

แล้ว restart mysql
แล้วอาจทดสอบด้วยการ telnet localhost 3307 ดู

และต้องเปลี่ยน phpMyAdmin อีกให้ มองเห็นตัวนี้ รวมทั้ง เมื่อจะติดต่อด้วย php
ต้องเรียกด้วย
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
หรือ
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');


แก้ไข ที่ phpMyAdmin ดังนี้
C:\AppServ\www\phpMyAdmin
config.inc.php แก้ไข จาก

cfg['Servers'][$i]['port'] = '';

ให้เป็น
cfg['Servers'][$i]['port'] = '3307';



แล้ว ลอง start mysql ใหม่
และ start phpmyadmin ใหม่ จาก browser

ถ้าทำการเปลี่ยน port ของ apache ต้อง restart apache ใหม่ด้วยครับ