JDBC如何执行'\。 sum sql file.sql'?

时间:2012-04-13 16:13:28

标签: mysql jdbc

如何使用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 */;

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

我要做的是查看打开文件,将其全部读入字符串然后执行。如果你遇到困难,快速谷歌搜索应该会帮助你。