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 jdbc;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class MyClass {
- public static void main(String[] args) throws SQLException {
- Connection connect = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- // connect
- connect = DriverManager.getConnection("jdbc:mysql://localhost/databasename"
- + "?user=root&password=");
- if (connect != null) {
- System.out.println("Database Connected.");
- } else {
- System.out.println("Database Connect Failed.");
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Statement stmt = connect.createStatement();
- ResultSet rs;
- String sql="SELECT * FROM customer";
- rs = stmt.executeQuery(sql);
- while ( rs.next() ) {
- String CustomerID = rs.getString("CustomerID");
- String Name = rs.getString("Name");
- System.out.println(CustomerID);
- System.out.println(Name);
- }
- String x;
- Scanner sc= new Scanner(System.in);
- try {
- if (connect != null) {
- connect.close();
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement