Execute SQL script files in Java

Source: Internet
Author: User

Assume that SQL statements must be executed regularly in the Java program. The source code must be modified and then compiled again when the original SQL statement or more statements are added due to the change of requirements. If you write SQL statements in a separate SQL script file and the Java program loads and executes them on a regular basis, you can only modify the SQL script file each time you change the file.

Java does not provide anything ready for use, so I wrote such an SQL script execution class sqlfileexecutor. Common SQL script files are supported, and "--" is used as the annotation prefix, semicolon ";" is used to separate statements. The/*... */format comment format of MySQL is not supported. The script files edited in Windows and Linux/Unix are all tested. The line breaks in these two system files are different. Windows is "\ r \ n", and Linux/Unix is "\ n ".

50 ./**
51. * pass in the connection to execute the SQL script file, so that it can be in the same thing as its external database operations
52. * @ Param conn incoming database connection
53. * @ Param sqlfile SQL script file
54. * @ throws exception
55 .*/
56. Public void execute (connection Conn, string sqlfile) throws exception {
57. Statement stmt = NULL;
58. List <string> sqllist = loadsql (sqlfile );
59. stmt = conn. createstatement ();
60. For (string SQL: sqllist ){
61. stmt. addbatch (SQL );
62 .}
63. Int [] rows = stmt.exe cutebatch ();
64. system. Out. println ("Row count:" + arrays. tostring (rows ));
65 .}
66.
67 ./**
68. * self-built connection to execute SQL files in independent transactions
69. * @ Param sqlfile SQL script file
70. * @ throws exception
71 .*/
72. Public void execute (string sqlfile) throws exception {
73. Connection conn = dbcenter. getconnection ();
74. Statement stmt = NULL;
75. List <string> sqllist = loadsql (sqlfile );
76. Try {
77. Conn. setautocommit (false );
78. stmt = conn. createstatement ();
79. For (string SQL: sqllist ){
80. stmt. addbatch (SQL );
81 .}
82. Int [] rows = stmt.exe cutebatch ();
83. system. Out. println ("Row count:" + arrays. tostring (rows ));
84. dbcenter. Commit (conn );
85.} catch (exception ex ){
86. dbcenter. rollback (conn );
87. Throw ex;
88.} finally {
89. dbcenter. Close (null, stmt, Conn );
90 .}
91 .}
92.
93. Public static void main (string [] ARGs) throws exception {
94. List <string> sqllist = new sqlfileexecutor (). loadsql (ARGs [0]);
95. system. Out. println ("Size:" + sqllist. Size ());
96. For (string SQL: sqllist ){
97. system. Out. println (SQL );
98 .}
99 .}
100 .}

Program function: load the SQL script file, remove the comment lines, and separate the SQL statements. Two execute () execution methods are provided, that is, database connection and no connection. The method of passing in the connection allows it to operate in the same thing as that of an external database. Of course, if you can use other mechanisms to ensure the atomicity of the transaction, you can also. The other is to manage database resources by yourself. In this example, the dbcenter class is left for readers to implement by themselves, including obtaining, disabling connections, and submitting and rolling back transaction methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.