Created
November 18, 2013 16:50
-
-
Save tnhansel/7531151 to your computer and use it in GitHub Desktop.
MobileComputer Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package computer; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author hanseltan | |
*/ | |
public class DatabaseApplication { | |
Connection connection; | |
private final Statement statement; | |
private ResultSet resultSet; | |
//this is the connection for the method | |
public DatabaseApplication() throws ClassNotFoundException, SQLException{ | |
// Load the JDBC driver | |
Class.forName("com.mysql.jdbc.Driver"); | |
System.out.println("Driver loaded"); | |
// Connect to a database | |
connection = DriverManager.getConnection | |
("jdbc:mysql://localhost/Dealership" , "hansel", "password"); | |
System.out.println("Database connected"); | |
// Create a statement | |
statement = connection.createStatement(); | |
} | |
//this is the statment for the view method | |
public void view(String serialNum) throws SQLException{ | |
resultSet = statement.executeQuery("select * from MobileComputer where serialNum = "+serialNum); | |
// Iterate through the result and print the serial number | |
while (resultSet.next()) | |
System.out.println(resultSet.getString(1) + "\t" + | |
resultSet.getString(2) + "\t" + resultSet.getString(3)+ "\t"+ resultSet.getString(4)); | |
} | |
public void insert(String serial, String make, String model, String color ) throws SQLException{ | |
String stmnt = "insert into MobileComputer values ('"+serial+"', '"+make+"', '"+model+"', '"+color+"',null,null,null,null)"; | |
System.out.println(stmnt); | |
statement.executeUpdate(stmnt); | |
} | |
public void delete(String serialNum) throws SQLException{ | |
String stmnt =("delete from MobileComputer where serialNum=" +serialNum); | |
System.out.println(stmnt); | |
statement.executeUpdate(stmnt); | |
System.out.println("Coloumn has been deleted"); | |
//resultSet=statement.executeQuery("delete from MobileComputer where serialNum="+serialNum); | |
//while(resultSet.next()) | |
//System.out.println(resultSet.getString(1)+"\t"+resultSet.getString(2)+"\t"+resultSet.getString(3)+"\t"+resultSet.getString(4)); | |
} | |
public void close() throws SQLException{ | |
// Close the connection | |
connection.close(); | |
} | |
//method to update the tables in the database side | |
public static void main(String[] args) throws ClassNotFoundException, SQLException { | |
DatabaseApplication database = new DatabaseApplication(); | |
//database.insert("123","dell","laptop","black"); | |
database.view("123"); | |
database.delete("123"); | |
database.close(); | |
} | |
} |
trus di specification documentnya dia bilang you need to view the data first before deleting.. jadi inside delete method put view method inside it..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
delete line 56-58 kalo ga dipake