Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.Statement;
- public class towarydb {
- public static void main(String[] args) throws Exception
- {
- createTable();
- post();
- }
- public static void post() throws Exception
- {
- final String var1 = "56";
- final String var2 = "10";
- final String var3 = "2.50";
- try
- {
- Connection con = getConnection();
- PreparedStatement posted = con.prepareStatement("INSERT INTO towar(wzor,ilosc,cena) VALUES('"+var1+"' , '"+var2+"', '"+var3+"')");
- posted.executeUpdate();
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- finally
- {
- System.out.println("Dodano");
- }
- }
- public static void createTable() throws Exception
- {
- try
- {
- Connection con = getConnection();
- 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))");
- create.executeUpdate();
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- finally
- {
- System.out.println("utworzono prawidlowo");
- }
- }
- public static Connection getConnection() throws Exception
- {
- try
- {
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/towary";
- String username = "root";
- String password = "w4t3q99j";
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url,username,password);
- System.out.println("polączono");
- return conn;
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement