Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to use sql file in java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.ibatis.jdbc.ScriptRunner;
public class RunningScripts {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/talakai_noppi";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Initialize the script runner
      ScriptRunner sr = new ScriptRunner(con);
      //Creating a reader object
      Reader reader = new BufferedReader(new FileReader("E:sampleScript.sql"));
      //Running the script
      sr.runScript(reader);
   }
}
Comment

how to use sql file in java

import java.io.BufferedReader;
 
import java.io.File;
import java.io.FileReader;
 
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
 
public class Database  {
 
    private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
 
    static
    {
        try
        {
            Class.forName(DRIVER_NAME).newInstance();
            System.out.println("*** Driver loaded");
        }
        catch(Exception e)
        {
            System.out.println("*** Error : "+e.toString());
            System.out.println("*** ");
            System.out.println("*** Error : ");
            e.printStackTrace();
        }
 
    }
 
    private static final String URL = "jdbc:mysql://localhost/mydatabase";
    private static final String USER = "root";
    private static final String PASSWORD = "password";
    private static String INSTRUCTIONS = new String();
 
    public static Connection getConnection() throws SQLException
    {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
 
    public static void resetDatabase() throws SQLException
    {
        String s            = new String();
        StringBuffer sb = new StringBuffer();
 
        try
        {
            FileReader fr = new FileReader(new File("mySQLFile.sql"));
            // be sure to not have line starting with "--" or "/*" or any other non aplhabetical character
 
            BufferedReader br = new BufferedReader(fr);
 
            while((s = br.readLine()) != null)
            {
                sb.append(s);
            }
            br.close();
 
            // here is our splitter ! We use ";" as a delimiter for each request
            // then we are sure to have well formed statements
            String[] inst = sb.toString().split(";");
 
            Connection c = Database.getConnection();
            Statement st = c.createStatement();
 
            for(int i = 0; i<inst.length; i++)
            {
                // we ensure that there is no spaces before or after the request string
                // in order to not execute empty statements
                if(!inst[i].trim().equals(""))
                {
                    st.executeUpdate(inst[i]);
                    System.out.println(">>"+inst[i]);
                }
            }
   
        }
        catch(Exception e)
        {
            System.out.println("*** Error : "+e.toString());
            System.out.println("*** ");
            System.out.println("*** Error : ");
            e.printStackTrace();
            System.out.println("################################################");
            System.out.println(sb.toString());
        }
 
    }
Comment

PREVIOUS NEXT
Code Example
Java :: list of numbers java 
Java :: declare class constant array java 
Java :: dequeue in java 
Java :: get device token firebase 
Java :: Count Occurrences in Seven Integers Using Java Single Dimension Arrays 
Java :: random number generator java with range 
Java :: glide dependency 
Java :: java map get key from value 
Java :: java long to hours minutes and seconds 
Java :: java 8 find in list 
Java :: java 2d array length 
Java :: java add elements array 
Java :: upper en java 
Java :: String remove duplicate method in java 
Java :: how to round double to 2 decimal places java 
Java :: tomcat 9 default username and password 
Java :: java sql query with parameters 
Java :: navigation view menu color android 
Java :: how to copy array in java 
Java :: mouselistener in java 
Java :: android ecode base64 
Java :: how to get current time and date in android 
Java :: java substring based on delimiter 
Java :: java program for try catch finally 
Java :: writing values in 2d array java 
Java :: basic java programs 
Java :: button onclick java 
Java :: reverse number java 
Java :: inbuild method to sum of an arraylist elements in java 
Java :: remove duplicates from string in java 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =