※apt安裝方式會自動把環境改成openJDK
Step1.先升級套件
- sudo apt-get update
- sudo apt-get install libmysql-java
Step3.設定環境變數
你可以設定在全體環境的/etc/profile或是個別使用者的/home/[user]/.bashrc。
- sudo gedit /etc/profile
加入以下文字:
#MySQL JDBC environment
export CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar
Step4.載入新的環境變數設定
- source /etc/profile
Step5.看看現在的CLASSPATH
- echo $CLASSPATH
Step6.寫一個JAVA程式測試看看JDBC
package com.jt;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBDemo
{
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://127.0.0.1";
private static final String username = "yourDB_username";
private static final String password = "yourDB_password";
/**
* @param args
*/
public static void main(String[] args) throws ClassNotFoundException,SQLException
{
Connection conn;
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
System.out.println("Success");
conn.close();
}
}
記得在eclipse中要把JDBC的jar給加進來
- Select Project Properties > Java Build Path
- Select Libries tab
- Click Add External Jars
- Choose the jar file, /usr/share/java/mysql.jar
加進來後可以看到:
參考資料:
安裝說明:
沒有留言:
張貼留言