我需要从java代码中查找h2数据库服务器是否正在运行。我尝试过 getStatus().isRunning(args) 方法,但即使服务器正在运行,它也始终显示服务器未运行。下面是我的代码:
Server server = Server.createTcpServer(args);
// Find whether is server is on or not using "isRunning()" method
if (server.isRunning(false)) {
System.out.println("server is running");
} else {
System.out.println("server is not running");
}
// Find whether is server is on or not using "getStatus()" method
String statVariable = server.getStatus();
System.out.println("STATUS=" + statVariable);
System.out.println("SERVER GONNA START");
server.start();
最佳答案
也许您可以将请求发送到有问题的端口,假设它是 WebServer 或 TCP 服务器(默认端口分别为 8082 或 9092)。你知道这个电话
drewmac:bin Drawpierce$ java -cp h2*.jar org.h2.tools.Server
创建 3 个服务器并输出如下内容:
TCP 服务器运行于 tcp://192.168.1.3:9092
(仅限本地连接)
PG 服务器运行于 pg://192.168.1.3:5435
(仅限本地连接)
Web 控制台服务器在 http://192.168.1.3:8082
上运行(仅限本地连接)
然后,如果您调用例程来显示服务器上带有监听套接字的端口 我的电话是 drewmac:~drewpierce$ sudo lsof -i -P | sudo lsof -i -P | sudo lsof -i -P | sudo lsof -i -P | grep -i“听”
java 81339 Drawpierce 19u IPv6 0xffffff800b782ac0 0t0 TCP *:9092(监听)
java 81339 Drawpierce 22u IPv6 0xffffff800b781bc0 0t0 TCP *:5435(监听)
java 81339 Drawpierce 24u IPv6 0xffffff8015620800 0t0 TCP *:8082(监听)
现在,如果您想测试 Web 服务器(http 和 html 流到端口 80、8082,无论您做什么,您都可以发出 Chunk A。如果您想测试 TCP 服务器,您可以发出 Chunk B。
调用 block A,如java GreetingClient localhost 8082
或类似java GreetingClient ec2-1-2-3-4-amaz-aws-ec2.amazon.com 8082
不要忘记这个测试中的2个参数,否则会呕吐
block A:
import java.io.*;
import java.net.*;
public class GreetingClient {
public static void main(String[] args) {
// declaration section:
// mySocket: our client socket pretending to be a browser
// os: output stream
// is: input stream
Socket mySocket = null;
DataOutputStream os = null;
DataInputStream is = null;
String serverName = args[0];
int port = Integer.parseInt(args[1]);
// Initialization section:
// btw make sure parameters are passed noting that this quick code is NOT
// Try to open input and output streams
System.out.println("*1");
try {
mySocket = new Socket(serverName,port);
os = new DataOutputStream(mySocket.getOutputStream());
is = new DataInputStream(mySocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host: hostname");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: hostname");
}
System.out.println("*2");
// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on port 80, 8082, whatever
// (what the server is listening on)
if (mySocket != null && os != null && is != null) {
try {
// pretend to be a browser and do a GET against a resource
System.out.println("*3");
os.writeBytes("GET /index.html HTTP/1.0\r\n\r\n");
System.out.println("*4");
// wait for response from webserver, dump out response for sanity check
String responseLine;
while ((responseLine = is.readLine()) != null) {
System.out.println("Server: " + responseLine);
if (responseLine.indexOf("Ok") != -1) {
break;
}
}
// clean up:
// close the output stream
// close the input stream
// close the socket
System.out.println("*5");
os.close();
is.close();
mySocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
System.out.println("*6");
}
}
block A输出(至少对我来说):
*1
*2
*3
*4
Server: HTTP/1.1 200 OK
Server: Content-Type: text/html
Server: Cache-Control: no-cache
Server: Content-Length: 937
Server:
Server: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Server: <!--
Server: Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
Server: and the EPL 1.0 (http://h2database.com/html/license.html).
Server: Initial Developer: H2 Group
Server: -->
Server: <html><head>
Server: <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
Server: <title>H2 Console</title>
Server: <link rel="stylesheet" type="text/css" href="stylesheet.css" />
Server: <script type="text/javascript">
Server: location.href = 'login.jsp?jsessionid=f3d05d9b68f4c5407054628f096ffccb';
Server: </script>
Server: </head>
Server: <body style="margin: 20px;">
Server:
Server: <h1>Welcome to H2</h1>
Server: <h2>No Javascript</h2>
Server: If you are not automatically redirected to the login page, then
Server: Javascript is currently disabled or your browser does not support Javascript.
Server: For this application to work, Javascript is essential.
Server: Please enable Javascript now, or use another web browser that supports it.
Server:
Server: </body></html>
*5
*6
有一些事情,显然这是 H2 输出。 block 源代码可以缩减到大约 10 行。
block B(与 TCP jdbc 服务器对话 jdbc)
//STEP 1. Import required packages
import java.sql.*;
import org.h2.Driver;
public class JdbcTrial {
// JDBC driver name and database URL
//static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//static final String DB_URL = "jdbc:mysql://127.0.0.1/test";
static final String JDBC_DRIVER = "org.h2.Driver";
static final String DB_URL = "jdbc:h2:tcp://localhost/~/test";
// Database credentials
static final String USER = "sa";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
System.out.println("***** 1");
Class.forName(JDBC_DRIVER);
System.out.println("***** 2");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, FirstName, LastName from people";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
String first = rs.getString("FirstName");
String last = rs.getString("LastName");
//Display values
System.out.print("ID: " + id);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample
block B 输出:
drewmac:~ drewpierce$ java JdbcTrial
***** 1
***** 2
Connecting to database...
Creating statement...
ID: 1, First: joan, Last: london
ID: 2, First: Sgt., Last: Corholio
Goodbye!
只需修改 jdbc_driver 和 db_url,就可以在 mysql、mariadb 和 H2 上正常工作
您可以只进行套接字连接,而不发出数据检索调用,并真正修剪它。
至于如何使用 H2 getStatus 执行此操作,我不知道。祝你好运。
关于h2 - 如何从java代码中查找h2数据库服务器是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27834797/