Java中使用MySQL從安裝、配置到實際程式測試詳解

來源:互聯網
上載者:User
By zieckey(zieckey@yahoo.com.cn) 

                All Rights Reserved!                               這裡假設你的Java開發環境已經搭建好了。一、準備工作:下載MySQL:mysql-5.0.27-win32.zip
下載MySQL的JDBC jar包:mysql-connector-java-5.0.3-bin.jar二、安裝和測試MySQL MySQL在Windows下的安裝很簡單,注意在選擇配置的有一項是將MySQL的配置匯入系統內容變數中,
為了後面的方便這個選項要選項。
 安裝完後,下面測試下:
1. 運行MySQL Server
M:\Documents and Settings\apple>mysqld-nt --help
mysqld-nt  Ver 5.0.22-community-nt for Win32 on ia32 (MySQL Community Edition (G
PL))
Copyright (C) 2000 MySQL AB, by Monty and others
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL licenseStarts the MySQL database serverUsage: mysqld-nt [OPTIONS]For more help options (several pages), use mysqld --verbose --help 這裡可以看到mysqld-nt.exe是MySQL database server
我們再運行用戶端進行串連:如果這裡說系統沒有這個mysqld-nt命令,那麼很可能是MySQL的配置資訊沒有匯入系統內容變數中。這個時候可以將MySQL的安裝目錄下bin目錄匯入到系統PATH環境變數中,例如:
M:\WINDOWS\system32;M:\WINDOWS;M:\WINDOWS\System32\Wbem;M:\Program Files\MySQL\MySQL Server 5.0\bin
再運行 mysqld-nt 應該就沒有問題了。M:\Documents and Settings\apple>mysqld-nt2.串連MYSQL。
格式: mysql -h主機地址 -u使用者名稱 -p使用者密碼
例1:串連到本機上的MYSQL。
   首先在開啟DOS視窗,然後進入目錄 mysqlbin,再鍵入命令mysql -uroot -p,斷行符號後提示你輸密碼,如果剛安裝好MYSQL,超級使用者root是沒有密碼的,故直接斷行符號即可進入到MYSQL中了,MYSQL的提示符是:mysql>    M:\Documents and Settings\apple>mysql -uroot -p
 Enter password:
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 1 to server version: 5.0.22-community-nt Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>例2:串連到遠程主機上的MYSQL。假設遠程主機的IP為:110.110.110.110,使用者名稱為root,密碼為abcd123。則鍵入以下命令:
  mysql -h110.110.110.110 -uroot -pabcd123
  (注:u與root可以不用加空格,其它也一樣)3、退出MYSQL命令: exit (斷行符號)
mysql> exit
ByeM:\Documents and Settings\apple>三、在Java中使用MySQL資料庫
1. 准資料庫檔案
建立一個LearnJava資料庫:
M:\Documents and Settings\apple>mysql -uroot -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.0.22-community-ntType 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> create database LearnJava;
Query OK, 1 row affected (0.02 sec)建立UserInfo表格:
mysql> use LearnJava;
Database changed
mysql> create table UserInfo(
    -> UserName varchar (20) not null,
    -> UserPwd varchar (20) not null
    -> );
Query OK, 0 rows affected (0.06 sec)mysql>
插入一條資料:
mysql> insert into UserInfo values ('zieckey','123456');
Query OK, 1 row affected (0.03 sec)mysql>2. 將MySQL的JDBC jar包匯入系統內容中
這裡我是將mysql-connector-java-5.0.3-bin.jar放在java的安裝目錄下,
然後匯入到CLASSPATH環境變數中,例如:
.;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\mysql-connector-java-5.0.3-bin.jar;%JAVA_HOME%\lib\dt.jar;3. 建立 Java 程式 QueryJDBC.java

/**
 *    該程式用來測試JDBC和MySQL串連
 */
package test;

import java.sql.*;

public class QueryJDBC
{
    static
    {
        try
        {
            //驅動和odbc不同

            Class.forName ( "org.gjt.mm.mysql.Driver" );
            System.out.println ( "success loading mysql Driver...." );
        } catch ( Exception e )
        {
            System.out.println ( "Error loading mysql Driver....." );
            e.printStackTrace ( );
        }
    }

    /**
     * @param args
     */
    public static void main ( String agrs[] )
    {
        try
        {
            //串連參數與Access不同

            String url = "jdbc:mysql://localhost/LearnJSP";
            //建立串連

            Connection con = DriverManager.getConnection ( url, "root", "011124" );
            //建立發送SQL命令的Statement對象

            Statement stmt = con.createStatement ( );
            //返回查詢結果

            ResultSet rs = stmt.executeQuery ( "select * from userinfo" );

            ResultSetMetaData md = rs.getMetaData ( );

            int col = md.getColumnCount ( );
            for ( int i = 1; i <= col; i++ )
            {
                System.out.println ( md.getColumnName ( i ) + "\t" );
            }
            while ( rs.next ( ) )
            {
                String strData = "";
                for ( int i = 1; i <= col; i++ )
                {
                    strData = strData + rs.getString ( i ) + "\t";
                }
                System.out.println ( strData );
            }
            //斷開Connection串連

            rs.close();
            stmt.close();
            con.close ( );
        } catch ( SQLException e )
        {
            e.printStackTrace ( );
        }
    }

}

E:\JavaLesson\MySQLTest\test>javac QueryJDBC.java -d .

E:\JavaLesson\MySQLTest\test>java test.QueryJDBC
success loading mysql Driver....
UserName
UserPwd
zieckey 123456這裡要特別提醒一下,如果在Eclipse下建立工程的,
該程式很可能運行出現異常,提示找不到org.gjt.mm.mysql.Driver,
這個可以通過 Project->Properties->Java Build Path->Libraries 選擇 Add Extenal JARs,
然後就可以選擇第三方jar包:mysql-connector-java-5.0.3-bin.jar
這樣程式就可以正常運行了。 註:出處http://zieckey.cublog.cn
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.