Results 1 to 4 of 4
  1. #1
    megabull is offline Member
    Join Date
    Aug 2012
    Posts
    22
    Rep Power
    0

    Exclamation How to execute mysql command from text file

    I am currently making a small java console application which reads a sql command from a file and executes it on mysql. Now, am unable to do this(though i've done similar for Turbo C++). I'm new to Java, hence need some help from u guys. Am using Eclipse Helios for developing it.

    I'm mentioning the code for the application. Kindly see and lemme know what needs to be changed. The program is compiling, but showing any code

    Command in sqlcommand.txt - select * from table;

    Java Code:
    import java.io.*;
    import java.sql.*;
    public class Exectable {
    	public static void main(String[] args) 
    		{
    	  Connection con = null;
      	  String url = "jdbc:mysql://localhost:3306/";
    	  String db = "demo";
    	  String driver = "com.mysql.jdbc.Driver";
    	  try{
    	  Class.forName(driver);
    	  con = DriverManager.getConnection(url+db,"root","magic");
    	  Statement st = con.createStatement();
    
    	FileInputStream fstream = new FileInputStream("D:/TCM/ws/jdbcdemo/src/sqlcommand.txt");
    	  // Get the object of DataInputStream
    	  DataInputStream in = new DataInputStream(fstream);
    	  BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	  String strLine;
    	  //Read File Line By Line
    	  while ((strLine = br.readLine()) != null)   {
    	  // Print the content on the console
    		  {
      		  }
    		  st.execute(strLine);
    		  
    	  }
    }
    	catch(Exception e){
    		System.out.println(e);
    	}  finally
    	  {
    	  }}}
    Last edited by quad64bit; 08-24-2012 at 02:46 PM. Reason: Added code tags for OP

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: How to execute mysql command from text file

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to execute mysql command from text file

    The program is compiling, but showing any code
    Do you mean it's not showing any code?

    Also, don't use dataInputStreams for reading a file. Java Memes Which Refuse to Die | Javalobby See item number 2 - tl;dr:
    Java Code:
    BufferedReader br = new BufferedReader(new FileReader("filename.txt"));
    Then you say:
    Java Code:
    // Print the content on the console
              {
              }
              st.execute(strLine);
    What do those empty { } do? I don't see any statements that print anything, so thats why you see nothing printed.

  4. #4
    megabull is offline Member
    Join Date
    Aug 2012
    Posts
    22
    Rep Power
    0

    Default Re: How to execute mysql command from text file

    Quote Originally Posted by quad64bit View Post
    Do you mean it's not showing any code?

    Also, don't use dataInputStreams for reading a file. Java Memes Which Refuse to Die | Javalobby See item number 2 - tl;dr:
    Java Code:
    BufferedReader br = new BufferedReader(new FileReader("filename.txt"));
    Then you say:
    Java Code:
    // Print the content on the console
              {
              }
              st.execute(strLine);
    What do those empty { } do? I don't see any statements that print anything, so thats why you see nothing printed.


    Yeah, its NOT showing any code. My bad, guess it must have got deleted while editing the code. I tried putting the statement in before trying this, but it showed 'true' as output. And i want the query's output.
    Last edited by megabull; 08-24-2012 at 06:22 PM.

Similar Threads

  1. java execute command every 10 seconds
    By bizkit1 in forum Android
    Replies: 2
    Last Post: 05-25-2012, 08:09 PM
  2. Execute Cygwin command from Java app
    By ZebraIzToronto in forum Advanced Java
    Replies: 1
    Last Post: 01-21-2011, 05:51 AM
  3. Error execute command through jsp
    By rahid in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-21-2010, 03:20 AM
  4. Replies: 3
    Last Post: 04-26-2010, 09:00 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •