java连接msqlsql执行10gSQL文件
时间: 2023-09-15 14:20:38 浏览: 36
可以使用JDBC(Java Database Connectivity) API来连接MySQL数据库并执行SQL文件。以下是一个基本的示例代码:
```java
import java.sql.*;
public class MySQLConnection {
static final String DB_URL = "jdbc:mysql://localhost/your_database_name";
static final String USER = "your_username";
static final String PASS = "your_password";
public static void main(String[] args) {
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
Scanner scanner = new Scanner(new File("path/to/your/sql/file.sql"))) {
while(scanner.hasNextLine()) {
String sql = scanner.nextLine();
stmt.executeUpdate(sql);
}
System.out.println("SQL file executed successfully.");
} catch(SQLException e) {
e.printStackTrace();
} catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
```
请注意,上面的代码假定您已经安装了JDBC驱动程序并将其添加到您的类路径中。如果您还没有安装驱动程序,则需要下载并安装MySQL JDBC驱动程序。