Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package connect;
- import java.sql.*;
- public class MY_Database {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String url = "jdbc:mysql://localhost:3306/ ";
- String user = "root";
- String password = "";
- try
- {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection con= DriverManager.getConnection(url,user,password);
- Statement stt = con.createStatement();
- //Create and Save db
- stt.execute("Create Database");
- stt.execute("Use the test ");
- //Create out table
- stt.execute("DROP TABLE IF EXIST people");
- stt.execute("CREATE TABLE people("
- +"id BIGINT NOT NULL AUTO_INCREMENT,"
- +"fname VARCHAR(25), "
- +"lname VARCHAR (25),"
- +"PRIMARY KEY (id)"
- +
- " )");
- // ADD SOME ENTRIES
- stt.execute("INSERT INTO people(fname , lname ) VALUES "+
- "('Nathan','Tenderere') ('Praise ''Tenderere') ('Tadiwa
- ''Machona')");
- // Get people with surname Tenderere
- ResultSet res = stt.executeQuery("SELECT * FROM people WHERE lname =
- 'Tenderere' ");
- while (res.next()) {
- System.out.println(res.getString("fname")+""+
- res.getString("lname") );
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment