特殊:Badtitle/NS100:JDBCAndMySQL
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/JDBCAndMySQL }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/af | • {{#if: UbuntuHelp:JDBCAndMySQL|Afrikaans| [[::JDBCAndMySQL/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/ar | • {{#if: UbuntuHelp:JDBCAndMySQL|العربية| [[::JDBCAndMySQL/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/az | • {{#if: UbuntuHelp:JDBCAndMySQL|azərbaycanca| [[::JDBCAndMySQL/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/bcc | • {{#if: UbuntuHelp:JDBCAndMySQL|جهلسری بلوچی| [[::JDBCAndMySQL/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/bg | • {{#if: UbuntuHelp:JDBCAndMySQL|български| [[::JDBCAndMySQL/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/br | • {{#if: UbuntuHelp:JDBCAndMySQL|brezhoneg| [[::JDBCAndMySQL/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/ca | • {{#if: UbuntuHelp:JDBCAndMySQL|català| [[::JDBCAndMySQL/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/cs | • {{#if: UbuntuHelp:JDBCAndMySQL|čeština| [[::JDBCAndMySQL/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/de | • {{#if: UbuntuHelp:JDBCAndMySQL|Deutsch| [[::JDBCAndMySQL/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/el | • {{#if: UbuntuHelp:JDBCAndMySQL|Ελληνικά| [[::JDBCAndMySQL/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/es | • {{#if: UbuntuHelp:JDBCAndMySQL|español| [[::JDBCAndMySQL/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/fa | • {{#if: UbuntuHelp:JDBCAndMySQL|فارسی| [[::JDBCAndMySQL/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/fi | • {{#if: UbuntuHelp:JDBCAndMySQL|suomi| [[::JDBCAndMySQL/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/fr | • {{#if: UbuntuHelp:JDBCAndMySQL|français| [[::JDBCAndMySQL/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/gu | • {{#if: UbuntuHelp:JDBCAndMySQL|ગુજરાતી| [[::JDBCAndMySQL/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/he | • {{#if: UbuntuHelp:JDBCAndMySQL|עברית| [[::JDBCAndMySQL/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/hu | • {{#if: UbuntuHelp:JDBCAndMySQL|magyar| [[::JDBCAndMySQL/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/id | • {{#if: UbuntuHelp:JDBCAndMySQL|Bahasa Indonesia| [[::JDBCAndMySQL/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/it | • {{#if: UbuntuHelp:JDBCAndMySQL|italiano| [[::JDBCAndMySQL/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/ja | • {{#if: UbuntuHelp:JDBCAndMySQL|日本語| [[::JDBCAndMySQL/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/ko | • {{#if: UbuntuHelp:JDBCAndMySQL|한국어| [[::JDBCAndMySQL/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/ksh | • {{#if: UbuntuHelp:JDBCAndMySQL|Ripoarisch| [[::JDBCAndMySQL/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/mr | • {{#if: UbuntuHelp:JDBCAndMySQL|मराठी| [[::JDBCAndMySQL/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/ms | • {{#if: UbuntuHelp:JDBCAndMySQL|Bahasa Melayu| [[::JDBCAndMySQL/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/nl | • {{#if: UbuntuHelp:JDBCAndMySQL|Nederlands| [[::JDBCAndMySQL/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/no | • {{#if: UbuntuHelp:JDBCAndMySQL|norsk| [[::JDBCAndMySQL/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/oc | • {{#if: UbuntuHelp:JDBCAndMySQL|occitan| [[::JDBCAndMySQL/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/pl | • {{#if: UbuntuHelp:JDBCAndMySQL|polski| [[::JDBCAndMySQL/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/pt | • {{#if: UbuntuHelp:JDBCAndMySQL|português| [[::JDBCAndMySQL/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/ro | • {{#if: UbuntuHelp:JDBCAndMySQL|română| [[::JDBCAndMySQL/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/ru | • {{#if: UbuntuHelp:JDBCAndMySQL|русский| [[::JDBCAndMySQL/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/si | • {{#if: UbuntuHelp:JDBCAndMySQL|සිංහල| [[::JDBCAndMySQL/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/sq | • {{#if: UbuntuHelp:JDBCAndMySQL|shqip| [[::JDBCAndMySQL/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/sr | • {{#if: UbuntuHelp:JDBCAndMySQL|српски / srpski| [[::JDBCAndMySQL/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/sv | • {{#if: UbuntuHelp:JDBCAndMySQL|svenska| [[::JDBCAndMySQL/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/th | • {{#if: UbuntuHelp:JDBCAndMySQL|ไทย| [[::JDBCAndMySQL/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/tr | • {{#if: UbuntuHelp:JDBCAndMySQL|Türkçe| [[::JDBCAndMySQL/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/vi | • {{#if: UbuntuHelp:JDBCAndMySQL|Tiếng Việt| [[::JDBCAndMySQL/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/yue | • {{#if: UbuntuHelp:JDBCAndMySQL|粵語| [[::JDBCAndMySQL/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/zh | • {{#if: UbuntuHelp:JDBCAndMySQL|中文| [[::JDBCAndMySQL/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/zh-hans | • {{#if: UbuntuHelp:JDBCAndMySQL|中文(简体)| [[::JDBCAndMySQL/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:JDBCAndMySQL | UbuntuHelp:JDBCAndMySQL | {{#if: | :}}JDBCAndMySQL}}/zh-hant | • {{#if: UbuntuHelp:JDBCAndMySQL|中文(繁體)| [[::JDBCAndMySQL/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:JDBCAndMySQL|:JDBCAndMySQL|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :JDBCAndMySQL/zh | | {{#ifexist: JDBCAndMySQL/zh | | {{#ifeq: {{#titleparts:JDBCAndMySQL|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:JDBCAndMySQL|1|-1|}} | zh | | }}
For Ubuntu versions : 5.10, 6.06, 6.10, 7.04, 9.10
Introduction
This document explains how to set up Java applications to communicate with the MySQL Database. This can be used either in Java development, or applications which use JDBC such as OpenOffice.Org. See Using MySQL, JDBC and OpenOffice for more information. This document was written originally for 5.10, but has been updated for 6.06. Changes required for 5.10 are shown in italics It has also been checked against 6.10 (Edgy Eft) and 7.04 (Feisty Fawn), which should be installed as for 6.06
Installation
Install mysql client, server and the jdbc connector, either via synaptic or by using the following
sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install libmysql-java
On 5.10 you may need select "No configuration" for postfix if it is installed.
Set up MySQL default password
Set up the password for the root user as 'root' or whatever you want. The last entry is the password
mysqladmin -u root password root
Check you can connect to mysql
mysql -u root -p
then enter the password you just set and press return again. In this case it would be "root".
Create SQL Databases and tables
Create databases and tables or whatever, so you have some data to work with. You can disconnect and use mysql-query-browser for this if you wish. For the sample code below just enter
create database emotherearth;
User creation and privileges
Create a user with access to that table. Replace the items in square brackets by the database name, and the chosen user and password. Don't type in the square brackets !
grant all privileges on [database].* to [user]@localhost identified by "[password]"; flush privileges;
Note: Ubuntu 5.10 requires localhost.localdomain This is required because when you connect using "mysql -u root -p" it connects directly to the Sql Server. The "grant" line creates access via 127.0.0.1 (localhost). This is no longer an issue on 6.10. You can test this by
mysql -u [user] -h 127.0.0.1 -p
This will work only for the user you have just "granted", not for root. Counter-intuitively, the 'default' server and localhost/127.0.0.1 aren't the same thing
Setting up the user to use JDBC
Add these two lines to /home/[user]/.bashrc. I am running Java 5 which doesn't require the current directory to be on the Classpath ; I *think* Java 2 does, but I'm not going back to check it In that case you may need to have $CLASSPATH:.:/user/share/java/mysql.jar
CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar export CLASSPATH
Alternatively, you can set it for all users, by editing /etc/environment (use sudo - sudo vi /etc/environment)
CLASSPATH=".:/usr/share/java/mysql.jar"
Log out and Log in again. (If you only edited /home/[user]/.bashrc you don't need to log out/in, only execute in a terminal "$source .bashrc" in your home dir). Start up a terminal and type:
echo $CLASSPATH
It should print out something like ":/usr/share/java/mysql.jar"
Testing in Java
It should now work. Here is some typical code (clearing up removed for simplicity)
import java.sql.*; import java.util.Properties; public class DBDemo { // The JDBC Connector Class. private static final String dbClassName = "com.mysql.jdbc.Driver"; // Connection string. emotherearth is the database the program // is connecting to. You can include user and password after this // by adding (say) ?user=paulr&password=paulr. Not recommended! private static final String CONNECTION = "jdbc:mysql://127.0.0.1/emotherearth"; public static void main(String[] args) throws ClassNotFoundException,SQLException { System.out.println(dbClassName); // Class.forName(xxx) loads the jdbc classes and // creates a drivermanager class factory Class.forName(dbClassName); // Properties for user and password. Here the user and password are both 'paulr' Properties p = new Properties(); p.put("user","paulr"); p.put("password","paulr"); // Now try to connect Connection c = DriverManager.getConnection(CONNECTION,p); System.out.println("It works !"); c.close(); } }