Foro sobre Java SE > Ejecutable JAVA
Lo he intentado hacer como "conquistador", pero me da el mismo problema, me crea la carpeta "test-mxj" y dentro la carpeta "data" vacía.
En la línea:
conn = DriverManager.getConnection(url, userName, password);
Me da el error:
[MysqldResource] Mysqld not running. No file: C:\Users\yave\AppData\Local\Temp\test-mxj\data\MysqldResource.pid
EXCEPTION: Could not create socket factory 'com.mysql.management.driverlaunched.ServerLauncherSocketFactory' due to underlying exception:
Ahora he probado siguiendo el ejemplo:
http://dev.mysql.com/doc/connector-mxj/en/connector-mxj-configuration-java-object.html
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import com.mysql.management.MysqldResource;
import com.mysql.management.MysqldResourceI;
import com.mysql.management.util.QueryUtil;
public class ConnectorMXJObjectTestExample {
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
public static void main(String[] args) throws Exception {
try {
File ourAppDir = new File(System.getProperty(JAVA_IO_TMPDIR));
File databaseDir = new File(ourAppDir, "mysql-mxj");
int port = Integer.parseInt(System.getProperty("c-mxj_test_port", "3307"));
String userName = "root";
String password = "*******";
MysqldResource mysqldResource = startDatabase(databaseDir, port, userName, password);
Class.forName(DRIVER);
Connection conn = null;
try {
String dbName = "aaplicacion";
String url = "jdbc:mysql://localhost:" + port + "/" + dbName //
+ "?" + "createDatabaseIfNotExist=true"//
;
conn = DriverManager.getConnection(url, userName, password);
String sql = "SELECT VERSION()";
String queryForString = new QueryUtil(conn).queryForString(sql);
System.out.println("------------------------");
System.out.println(sql);
System.out.println("------------------------");
System.out.println(queryForString);
System.out.println("------------------------");
System.out.flush();
Thread.sleep(100); // wait for System.out to finish flush
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
mysqldResource.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.println("@@EXCEPTION: ### -" + Thread.currentThread().getStackTrace()[1].getMethodName() + "- ###\n--> " + e.getMessage());
}
}
public static MysqldResource startDatabase(File databaseDir, int port, String userName, String password) {
MysqldResource mysqldResource = new MysqldResource(databaseDir);
try {
Map database_options = new HashMap();
database_options.put(MysqldResourceI.PORT, Integer.toString(port));
database_options.put(MysqldResourceI.INITIALIZE_USER, "true");
database_options.put(MysqldResourceI.INITIALIZE_USER_NAME, userName);
database_options.put(MysqldResourceI.INITIALIZE_PASSWORD, password);
mysqldResource.start("test-mysqld-thread", database_options);
if (!mysqldResource.isRunning()) {
throw new RuntimeException("MySQL did not start.");
}
System.out.println("MySQL is running.");
} catch (Exception e) {
System.out.println("@@EXCEPTION: ### -" + Thread.currentThread().getStackTrace()[1].getMethodName() + "- ###\n--> " + e.getMessage());
}
return mysqldResource;
}
}
Pero... dentro de la función "startDatabase" me falla en la línea:
mysqldResource.start("test-mysqld-thread", database_options);
Mostrándome:
[C:\Users\yave\AppData\Local\Temp\mysql-mxj\bin\mysqld.exe][--no-defaults][--console][--port=3307][--socket=mysql.sock][--basedir=C:\Users\yave\AppData\Local\Temp\mysql-mxj][--datadir=C:\Users\yave\AppData\Local\Temp\mysql-mxj\data][--pid-file=C:\Users\yave\AppData\Local\Temp\mysql-mxj\data\MysqldResource.pid]
[MysqldResource] launching mysqld (test-mysqld-thread)
120704 15:52:22 [Note] Plugin 'FEDERATED' is disabled.
120704 15:52:22 InnoDB: The InnoDB memory heap is disabled
120704 15:52:22 InnoDB: Mutexes and rw_locks use Windows interlocked functions
120704 15:52:22 InnoDB: Compressed tables use zlib 1.2.3
120704 15:52:22 InnoDB: Initializing buffer pool, size = 128.0M
120704 15:52:22 InnoDB: Completed initialization of buffer pool
120704 15:52:22 InnoDB: highest supported file format is Barracuda.
120704 15:52:22 InnoDB: Waiting for the background threads to start
120704 15:52:23 InnoDB: 1.1.5 started; log sequence number 1595675
120704 15:52:23 [Note] Event Scheduler: Loaded 0 events
120704 15:52:23 [Note] C:\Users\yave\AppData\Local\Temp\mysql-mxj\bin\mysqld.exe: ready for connections.
Version: '5.5.9' socket: '' port: 3307 MySQL Community Server (GPL)
[MysqldResource] mysqld running as process: 3764
Y después en al ejecutar el "Finally" del "main", al ejecutar la línea:
mysqldResource.shutdown();
Me muestra:
[MysqldResource] stopping mysqld (process: 3764)
120704 15:52:28 [Note] C:\Users\yave\AppData\Local\Temp\mysql-mxj\bin\mysqld.exe: Normal shutdown
120704 15:52:28 [Note] Event Scheduler: Purging the queue. 0 events
120704 15:52:28 InnoDB: Starting shutdown...
120704 15:52:29 InnoDB: Shutdown completed; log sequence number 1595675
120704 15:52:29 [Note] C:\Users\yave\AppData\Local\Temp\mysql-mxj\bin\mysqld.exe: Shutdown complete
[MysqldResource] clearing options
[MysqldResource] shutdown complete
Con todas las líneas que empiezan por 120704 en rojo.
Nota: a "mysql server" le di el puerto 3307 para que no hubiera conflicto con el WAMP (y me funcionaba correctamente).
Alguna idea de cómo sería la forma correcta de hacerlo??
Gracias!!
Lo del .jar del connector-j.... ya tenía añadido el MySQL JDBC Driver - mysql-connector-java-5.1.13-bin.jar, por lo que ya está.