Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package dbconn;
- import java.sql.*;
- /**
- *
- * @author MYPC
- */
- public class DBConn {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- insert();
- select();
- numberPalindrome();
- reverseArray();
- stringPalindrome();
- }
- public static void insert(){
- String dbURL = "jdbc:mysql://localhost:3306/sampledb";
- String username = "root";
- String password = "";
- String query = "INSERT INTO Users(username, password, fullname, email) Values(?,?,?,?)";
- try {
- Connection conn = DriverManager.getConnection(dbURL, username, password);
- if (conn != null) {
- System.out.println("Connected");
- }
- PreparedStatement statement = conn.prepareStatement(query);
- statement.setString(1, "bill2222");
- statement.setString(2, "secretpass22");
- statement.setString(3, "bill gates22");
- statement.setString(4, "bill@gmail.com22");
- int rowsInserted = statement.executeUpdate();
- if (rowsInserted > 0) {
- System.out.println("Successfully entered");
- }
- conn.close();
- }catch(Exception ex) {
- System.out.println("Error: " + ex);
- }
- }
- public static void select() {
- String dbURL = "jdbc:mysql://localhost:3306/sampledb";
- String username = "root";
- String password = "";
- String query = "SELECT * from Users";
- try {
- Connection conn = DriverManager.getConnection(dbURL, username, password);
- if (conn != null) {
- System.out.println("Connected");
- }
- Statement statement = conn.createStatement();
- ResultSet result = statement.executeQuery(query);
- int count = 0;
- while (result.next()) {
- String name = result.getString(2);
- String pass = result.getString(3);
- String fullname = result.getString("fullname");
- String email = result.getString(5);
- String output = "User #%d: %s - %s - %s - %s";
- System.out.println(String.format(output, ++count, name, pass, fullname, email));
- }
- conn.close();
- }catch(Exception ex) {
- System.out.println("Error: " + ex);
- }
- }
- public static void numberPalindrome() {
- int number = 12345;
- int remainder = 0, sum = 0;
- while(number > 0) {
- remainder = number %10;
- sum = (sum * 10) + remainder;
- number /= 10;
- }
- System.out.println(sum);
- }
- public static void reverseArray() {
- int arr[] = {1, 2,3,4,5};
- for(int i = 0; i < arr.length/2; i++) {
- int temp = arr[i];
- arr[i] = arr[arr.length-1 -i];
- arr[arr.length-1 -i] = temp;
- }
- for(int n : arr) {
- System.out.print(n);
- }
- }
- public static void stringPalindrome() {
- String word = "123";
- String rev = "";
- // for(int i = word.length()-1; i >= 0 ; i--) {
- // rev += word.charAt(i);
- // }
- for(int i = 0; i < word.length();i++) {
- rev += word.charAt(word.length()-i -1);
- }
- System.out.println("\n"+rev);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement