0

您如何使用 jdbc 执行此类操作?

String x = ("\\. /home/user/Desktop/dbfile.sql");

Class.forName(Database.JDBC_DRIVER);
Connection conn = (Connection) DriverManager.getConnection(
    Database.DB_URL + "localhost" + "/" + "mydatabase", "root", "");
Statement stmt = (Statement) conn.createStatement();

stmt.execute(x);

结果:线程“main” com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 中的异常:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 '.

我试过这个

public static void main(String[] args) throws Exception
  {

    String x = readFileAsString("/home/user/Desktop/myDB.sql");

    Class.forName(Database.JDBC_DRIVER);
    Connection conn = (Connection) DriverManager.getConnection(
        Database.DB_URL + "localhost" + "/" + "mydb", "root", "");
    Statement stmt = (Statement) conn.createStatement();

    System.out.println(x);
    stmt.execute(x);

  }

  private static String readFileAsString(String filePath) throws Exception
  {
    byte[] buffer = new byte[(int) new File(filePath).length()];
    BufferedInputStream f = null;
    try
    {
      f = new BufferedInputStream(new FileInputStream(filePath));
      f.read(buffer);
    }
    finally
    {
      if (f != null)
        try
        {
          f.close();
        }
        catch (IOException ignored)
        {
        }
    }
    return new String(buffer);
  }

结果

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

我的文件看起来像

-- MySQL dump 10.13  Distrib 5.5.21, for Linux (x86_64)
--
-- Host: localhost    Database: DOG
-- ------------------------------------------------------
-- Server version   5.5.21

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `DOG`
--

DROP TABLE IF EXISTS `DOG`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `DOG` (
  `DOG_ID` mediumint(9) NOT NULL,
  `OWNER_ID` mediumint(9) NOT NULL,
  PRIMARY KEY (`DOG_ID`,`OWNER_ID`),
  KEY `CHANNEL_FK1` (`OWN_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
4

2 回答 2

3

你不能那样做。您需要打开dbfile.sql自己,并通过 JDBC API 运行所有行。

如果您每行有一个 SQL 语句,这可能会起作用:

Class.forName(Database.JDBC_DRIVER);
Connection conn = (Connection) DriverManager.getConnection(
    Database.DB_URL + "localhost" + "/" + "mydatabase", "root", "");
Statement stmt = (Statement) conn.createStatement();
BufferedReader reader = new BufferedReader( new FileReader ("/home/user/Desktop/dbfile.sql"));
String line  = null;
while( ( line = reader.readLine() ) != null ) {
    stmt.executeUpdate(line);    
}
stmt.close();
conn.close();
于 2012-04-13T16:15:42.100 回答
0

我要做的是查看打开文件,将其全部读入字符串然后执行。如果您卡在这里,快速的 Google 搜索应该会对您有所帮助。

于 2012-04-13T16:18:38.987 回答