日期:2014-05-16  浏览次数:20778 次

embeded mysql 用法示例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;

public class ConnectorMXJTestExample {
??? public static void main(String[] args) throws Exception {
??????? String hostColonPort = "localhost:3336";
???????
??????? String driver = com.mysql.jdbc.Driver.class.getName();
??????? String url = "jdbc:mysql://" + hostColonPort + "/" + "?"
??????????????? + "socketFactory="
??????????????? + ServerLauncherSocketFactory.class.getName();
??????? String userName = "root";
??????? String password = "";

??????? Class.forName(driver);
??????? Connection conn = null;
??????? try {
??????????? conn = DriverManager.getConnection(url, userName, password);
??????????? Statement stmt = conn.createStatement();
??????????? ResultSet rs = stmt.executeQuery("SELECT VERSION()");
??????????? rs.next();
??????????? String version = rs.getString(1);
??????????? rs.close();
??????????? stmt.close();

??????????? System.out.println("------------------------");
??????????? System.out.println(version);
??????????? System.out.println("------------------------");
??????? } finally {
??????????? try {
??????????????? conn.close();
??????????? } catch (Exception e) {
??????????????? e.printStackTrace();
??????????? }
??????????? ServerLauncherSocketFactory.shutdown(hostColonPort);
??????? }
??? }
}