English 中文(简体)
Java/Mysql 你们如何把整个文件包放到我的服务器上?
原标题:Java/Mysql How would you inject an entire SQL file to a mysql server?
  • 时间:2012-04-13 14:26:47
  •  标签:
  • java
  • mysql

How would you go about loading a .sql file into a mysql database from a java program? I ve tried extracting a sql resource file from the self containted jar, I ve try processBuilder to locate and run external shell commands. I ve tried about everything and none of which seem like the correct way about doing this.

www.un.org/spanish/ecosoc 因此,看一看问题,我只是这样做:。

在任何数据库中,均采用了我的假设:mysqldump -uroot -routines myDB >~/Desktop/myDB.sql

www.un.org/spanish/ecosoc 现在,请设法从java/jdbc一皮中恢复整个数据库。 如何适当和正确地用我的手提B.sql档案这样做?

It might be nice to think you create a nice jdbc statement to loop around your databse file but then it gets ugly and bunk when you do get a section of your dump like this

<>m>Problem, 如下所示:

/*!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`),
  KEY `CHANNEL_FK1` (`OWNER_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

www.un.org/spanish/ecosoc 这是否限制了jdbc? 是否有一只冰皮,我可以称之为我的sql.createDatabase(myDB.sql)。

问题回答
  1. Make sure the mysql connector is in your class path, for example java -cp mysql-connector-java-5.1.13-bin.jar or if you build your project with ant or NetBeans, make sure that the mysql connector is included as a library, that way when you build your project, java -jar YourApplication.jar should find lib/mysql-connector-java-5.1.13-bin.jar or what ever.
  2. In your source code, load the driver: Class.forName("com.mysql.jdbc.Driver").newInstance();
  3. Load your .sql file: String sql = new Scanner(new File("yourfile.sql")).useDelimiter("").next();
  4. Connect to your database: Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database",username,password);
  5. Execute your sql: Statement st = conn.createStatement();st.execute(sql);

以上假设是,你的文件库载有发言稿。

如果它不通过亚洲开发银行工作,我建议这样做。

通常,我使用“电离层电离层”进行这种工作(作为我的舱堆放档案):

<exec command="mysql -u ${db.user} --password=${db.password} 
                     --database=${db.name}">
    <redirector input="${sql.file}"/>
</exec>

从根本上说,你可以从你法典中构筑和 call忙的任务(如果你给你的班子增添必要的附属条件的话)。

我不相信它的最佳方式,而是在类似情况下对我的工作。

首先界定以下变量:

private static final String BASE_MYSQL_COMMAND_LINE_STR = "mysql -uUSER -pPASSWORD -h localhost < ";

请用真正的用户名称和密码取代“USER”和“PASORD”,或如果你不希望硬编码的话,只是动态地构造。 在实际价值之间没有空间。

界定以下职能:

private static void executeCommandInLinuxShell(String command) throws Exception {
            String[] cmd = {"/bin/sh", "-c", command};  
            Process proc = Runtime.getRuntime().exec(cmd);
            BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line;
            while ((line = bufferedreader.readLine()) != null);
            bufferedreader.close();
            proc.waitFor();

    }

因此,假定在你的方案中,你有一只“sqlFile”文档,注明“mydB.sql”db文档:

String scmd = BASE_MYSQL_COMMAND_LINE_STR + sqlFile.getAbsolutePath();
executeCommandInLinuxShell(scmd);

rel=“nofollow>>Flyway 有一个先进的梯子,可以处理箱子外的我的堆放场。 为您的使用而进行配置应是三维之策。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签