Advertisement
Guest User

SQL1

a guest
Oct 27th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package SOL_Server;
  2.  
  3. import java.sql.Statement;
  4. import java.sql.Connection;
  5. import java.sql.DatabaseMetaData;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class DBExample2 {
  12.     public static void main(String[] args) {
  13.         // TODO Auto-generated method stub
  14.         List<String> list1 = GetAllProductName();
  15.         for(String item : list1)
  16.         {
  17.             System.out.println(item);
  18.         }
  19.         InsertProduct("6","Product 6");
  20.        
  21.         list1 = GetAllProductName();
  22.         for(String item : list1)
  23.         {
  24.             System.out.print(item);
  25.         }
  26.     }
  27.    
  28.     public static List<String> GetAllProductName()
  29.     {
  30.             Connection conn = null;
  31.             ArrayList<String> prodcutlist = new ArrayList<String>();
  32.             try{
  33.                 String dbURL = "jdbc:sqlserver://localhost\\SQLEXPRESS;DatabaseName=ExampleDatabase1";
  34.                 String user = "sa";
  35.                 String pass = "t3stt3st";
  36.                 conn = DriverManager.getConnection(dbURL, user , pass);
  37.                 if(conn != null) {
  38.                     Statement m_Statement = conn.createStatement();
  39.                     String query = "SELECT * FROM product_tbl";
  40.                    
  41.                     ResultSet m_ResultSet = m_Statement.executeQuery(query);
  42.                    
  43.                     while(m_ResultSet.next()){
  44.                         prodcutlist.add(m_ResultSet.getString(1)+","+m_ResultSet.getString(2));
  45.                     }
  46.                 }
  47.             } catch (SQLException ex) {
  48.                 ex.printStackTrace();
  49.             } finally {
  50.                 try {
  51.                     if(conn != null && !conn.isClosed()) {
  52.                         conn.close();
  53.                     }
  54.                 } catch (SQLException ex) {
  55.                     ex.printStackTrace();
  56.                 }
  57.             }
  58.                   return prodcutlist;
  59.     }
  60.     public static void InsertProduct(String id,String name)
  61.     {
  62.             Connection conn = null;
  63.             ArrayList<String> prodcutlist = new ArrayList<String>();
  64.             try{
  65.                
  66.                 String dbURL = "jdbc:sqlserver://JAYAKRIT\\SQLEXPRESS;DatabaseName=happyWithDatabase ";
  67.                 String user = "sa";
  68.                 String pass = "t3stt3st";
  69.                 conn = DriverManager.getConnection(dbURL , user , pass);
  70.                 if(conn != null){
  71.                     Statement m_Statement = conn.createStatement();
  72.                     String query = "Insert into product_tbl (p) values('"+id+"','"+name+"') ";
  73.                     m_Statement.execute(query);
  74.                 }
  75.             } catch (SQLException ex){
  76.                 ex.printStackTrace();
  77.             } finally {
  78.                 try {
  79.                     if(conn != null && !conn.isClosed()){
  80.                         conn.close();
  81.                     }
  82.                 } catch (SQLException ex){
  83.                     ex.printStackTrace();
  84.                 }
  85.             }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement