Advertisement
Guest User

Example JDBC+SQL file

a guest
Dec 26th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. package sqlModifier;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.SQLException;
  11.  
  12. public class ModifierMain {
  13.  
  14.     public static void main(String[] args) {
  15.        
  16.         Connection dbConn=connect();
  17.         try {
  18.             PreparedStatement psmt=dbConn.prepareStatement(InputStencyl("dbCMDs/insert.sql"));
  19.             psmt.setString(1, "Sponge Bob");
  20.             psmt.setInt(2, 34);
  21.             psmt.setDouble(3,12.56);
  22.             psmt.executeUpdate();
  23.             System.out.println("good");
  24.         } catch (SQLException e) {
  25.             // TODO Auto-generated catch block
  26.             e.printStackTrace();
  27.         }
  28.    
  29.  
  30.     }
  31.    
  32.     public static String InputStencyl(String fileName){
  33.        
  34.         File f=new File(fileName);
  35.         try(
  36.                 FileReader fr=new FileReader(f);
  37.                 BufferedReader bfr=new BufferedReader(fr);
  38.                 ){
  39.             String stencyl="";
  40.            
  41.             while(true){
  42.                 String in=bfr.readLine();
  43.                 if(in==null){
  44.                     return stencyl;
  45.                 }
  46.                 else{
  47.                     stencyl+=in;
  48.                 };
  49.             }
  50.            
  51.         }catch(IOException IOE){
  52.                     IOE.printStackTrace();
  53.                     return "failed";
  54.                 }
  55.     }
  56.    
  57.    
  58.     public static Connection connect(){
  59.         try {
  60.             Connection c=DriverManager.getConnection("jdbc:mysql://127.0.0.1/course?&useSSL=true&useUnicode=true","root","mysqlFRS");
  61.             return c;
  62.         } catch (SQLException e) {
  63.             // TODO Auto-generated catch block
  64.             e.printStackTrace();
  65.             return null;
  66.         }
  67.        
  68.        
  69.        
  70.     }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement