Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.Statement;
  5.  
  6.  
  7. public class towarydb {
  8.    
  9.     public static void main(String[] args) throws Exception
  10.     {
  11.             createTable();
  12.             post();
  13.     }
  14.  
  15.     public static void post() throws Exception
  16.     {
  17.         final String var1 = "56";
  18.         final String var2 = "10";
  19.         final String var3 = "2.50";
  20.        
  21.         try
  22.         {
  23.             Connection con = getConnection();
  24.             PreparedStatement posted = con.prepareStatement("INSERT INTO towar(wzor,ilosc,cena) VALUES('"+var1+"' , '"+var2+"', '"+var3+"')");
  25.             posted.executeUpdate();
  26.            
  27.         }
  28.         catch(Exception e)
  29.         {
  30.             System.out.println(e);
  31.         }
  32.         finally
  33.         {
  34.             System.out.println("Dodano");
  35.         }
  36.     }
  37.    
  38.     public static void createTable() throws Exception
  39.     {
  40.         try
  41.         {
  42.             Connection con = getConnection();
  43.             PreparedStatement create = con.prepareStatement("CREATE TABLE IF NOT EXISTS towar(id int NOT NULL AUTO_INCREMENT, wzor varchar(255), ilosc int, cena float , PRIMARY KEY(id))");
  44.             create.executeUpdate();
  45.            
  46.         }
  47.         catch(Exception e)
  48.         {
  49.             System.out.println(e);
  50.         }
  51.         finally
  52.         {
  53.             System.out.println("utworzono prawidlowo");
  54.         }
  55.     }
  56.    
  57.     public static Connection getConnection() throws Exception
  58.     {
  59.        
  60.         try
  61.         {
  62.             String driver = "com.mysql.jdbc.Driver";
  63.             String url = "jdbc:mysql://localhost:3306/towary";
  64.            
  65.             String username = "root";
  66.             String password = "w4t3q99j";
  67.            
  68.             Class.forName(driver);
  69.            
  70.             Connection conn = DriverManager.getConnection(url,username,password);
  71.             System.out.println("polączono");
  72.             return conn;
  73.         }
  74.         catch(Exception e)
  75.         {
  76.             System.out.println(e);
  77.         }
  78.        
  79.         return null;
  80.        
  81.     }
  82.    
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement