Tag: port date () date manage string concatenation for EXCE teacher Package
Package com.lovo.test;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.ArrayList;
Import Javax.swing.JOptionPane;
Import Com.lovo.bean.ClassBean;
public class Testdml {
public static void Main (string[] args) {
String className = Joptionpane.showinputdialog ("Please enter class name");
String teachername = Joptionpane.showinputdialog ("Please enter the class name");
Database operation steps:
1. Load driver---Tell the Driver Manager which database we will use to drive the package
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
2. Operation of JDBC API to complete database action
2-1. Get the connection
Connection con = null;
try {
url--Uniform Resource Locator----style: protocol://IP Address: Port number/Service
con = drivermanager.getconnection ("jdbc:mysql://127.0.0.1:3306/test134" +
"? Useunicode=true&characterencoding=utf8&usessl=false", "root", "root");
2-2. Write SQL statements---string concatenation,
String sql = "INSERT into T_class (f_classname,f_teacher) VALUES ('" +classname+ "', '" +teachername+ "')";
2-3. Get the Statement object---statement object
Statement state = Con.createstatement ();
2-4, executes the statement object---All DML statements, all execute the executeupdate () method
int row = state.executeupdate (sql);//The int returned represents how many rows were affected!
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally{
2-5. Close the connection
if (con! = null) {
try {
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
}
JDBC Programming Example