public class Jdbcdemo {
public static void Main (string[] args) {
JDBC Six Footwork
try {
/** First step: Load driver */
String Driverurl = "Com.mysql.jdbc.Driver";
Class.forName (Driverurl);
SYSTEM.OUT.PRINTLN ("Load successfully!!!");
/** Step Two: Establish a connection */
String url = "Jdbc:mysql://localhost:3306/demo";
String user = "root";
String Password = "123";
Connection con = drivermanager.getconnection (url, user, password);
/** Step Three: Create a statement Object */
Statement Statement = Con.createstatement ();
/** Fourth Step: Execute SQL statement */
String sql = "INSERT into actor (ID, name) VALUES (5947, ' Zhang Four ')";
String sql = "SELECT * from actor where id >= 5946";
/** Fifth Step: Operation result set */
int row = statement.executeupdate (SQL);
if (Row > 0) {
System.out.println ("add success");
//}
ResultSet ResultSet = statement.executequery (sql);
set<actor> actors = new hashset<actor> ();
while (Resultset.next ()) {
int id = resultset.getint (1);
String name = resultset.getstring (2);
Actor actor = new actor (ID, name);
Actors.add (actor);
}
for (Actor ac:actors) {
System.out.println (Ac.getname ());
}
/** Sixth step: Close the object */
Resultset.close ();
Statement.close ();
Con.close ();
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}catch (SQLException e) {
E.printstacktrace ();
}
}
}
2016.3.30 (Java database programming)