java无法连接服务器,java无法连接 (解决方法与步骤)
下面内容仅为某些场景参考,为稳妥起见请先联系上面的专业技术工程师,具体环境具体分析。
2023-09-21 11:25 60
```java
import java.sql.*;
public class HiveConnectionExample {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) {
try {
// 加载Hive JDBC驱动程序
Class.forName(driverName);
// 建立连接
Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "username", "password");
// 创建Statement对象
Statement stmt = con.createStatement();
// 执行Hive查询,比如创建表
String tableName = "my_table";
String createTableQuery = "CREATE TABLE IF NOT EXISTS " + tableName + " (id INT, name STRING)";
stmt.execute(createTableQuery);
// 执行Hive查询,比如查询表数据
String selectQuery = "SELECT * FROM " + tableName;
ResultSet rs = stmt.executeQuery(selectQuery);
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name"));
}
// 关闭资源
rs.close();
stmt.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
您需要将`jdbc:hive2://localhost:10000/default`替换为您的Hive服务器的URL。还需要提供正确的用户名和密码。