Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Reviseknowledge;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- public class Learn {
- public static void main(String[] args) throws Exception {
- }
- static String url = "jdbc:mysql://localhost:3306/guest";
- static String username = "root";
- static String password = "guest123";
- public static void customSelection () throws Exception {
- try {
- Connection conn = DriverManager.getConnection(url,username,password);
- PreparedStatement custom = conn.prepareStatement ( "SELECT * from stebuklas where numbers < 10 ");
- ResultSet res = custom.executeQuery ();
- while ( res.next() ) {
- System.out.println ( res.getString ("first") + "\t" + res.getString ("last") + res.getInt("numbers") );
- }
- }
- catch(Exception e){ System.out.println(e); }
- }
- public static void update () throws Exception { // UPDATE UPDATE, remove old ones and replace them.
- try{
- Connection conn = DriverManager.getConnection(url,username,password);
- PreparedStatement update = conn.prepareStatement ( "UPDATE stebuklas set last='boboz' where numbers =7");
- update.executeUpdate();
- }
- catch(Exception e){ System.out.println(e); }
- System.out.println ("enri was replaced (updated)"); ////
- }
- public static void createTable () throws Exception { // CREATE CREATE NEW TABLET
- try {
- Connection conn = DriverManager.getConnection(url,username,password);
- PreparedStatement create = conn.prepareStatement("CREATE TABLE IF NOT EXISTS stebuklas "
- + "( first varchar(253), "
- + "last varchar(250),"
- + "numbers integer(50) )");
- create.executeUpdate();
- }
- catch(Exception e){ System.out.println(e); }
- finally { System.out.println ("table was madee"); } ////
- }
- public static void post () throws Exception { // INSERT INSERT VALUES
- String var1 = "enri";
- String var2 = "lerp";
- double var3 = 7;
- try {
- Connection conn = DriverManager.getConnection(url,username,password);
- PreparedStatement posted = conn.prepareStatement (" INSERT INTO stebuklas "
- + "(first,last,numbers) VALUES ('"+var1+"', '"+var2+"', '"+var3+"')");
- posted.executeUpdate ();
- posted.executeUpdate ();
- }
- catch(Exception e){ System.out.println(e); }
- finally {System.out.println ("values inserted to the table.");} //////
- }
- public static void get() throws Exception { // SELECT SELECT SELECT
- try{
- Connection conn = DriverManager.getConnection(url,username,password);
- PreparedStatement statement = conn.prepareStatement("SELECT * FROM stebuklas LIMIT 2");
- PreparedStatement stat = conn.prepareStatement("SELECT * FROM stebuklas LIMIT 3");
- ResultSet result = statement.executeQuery ();
- ResultSet res = stat.executeQuery ();
- while (result.next() ) {
- System.out.println ( result.getString("first") );
- }
- while ( res.next() ) {
- System.out.println ( res.getInt("numbers") );
- }
- }
- catch(Exception e){ System.out.println(e); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement