Insert a data entry into the database using JDBC (the first modified version), and The jdbc modified version
Added a Tools class and added some common Tools.
Package com. JDBC. java; import java. io. IOException; import java. io. inputStream; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. statement; import java. util. properties;/*** JDBC tool class */public class JDBCTools {/*** close Statement, connection ** @ param statement * @ param connection */public static void release (Statement statement, Connection connection) {If (statement! = Null) {try {statement. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (connection! = Null) {try {connection. close ();} catch (SQLException e) {e. printStackTrace () ;}}/ *** method for obtaining database connections ** @ return * @ throws Exception */public static Connection getConnection () {// four strings to connect to the database // The full Driver Class Name String driverClass = null; String jdbcUrl = null; String user = null; String password = null; string jdbcName = null; // read jdbcName. properties file InputStream inStream = JDBCTools. class. getClassLoader (). getResourceAsStream ("properties/jdbcName. properties "); Properties propertiesOfName = new Properties (); try {propertiesOfName. load (inStream);} catch (IOException e) {e. printStackTrace ();} jdbcName = propertiesOfName. getProperty ("jdbcName"); // read the required properties file InputStream in = JDBCTools. class. getClassLoader (). getResourceAsStream ("properties/" + jdbcName + ". properties "); Properties properties = new Properties (); try {properties. load (in);} catch (IOException e) {e. printStackTrace ();} driverClass = properties. getProperty ("driver"); jdbcUrl = properties. getProperty ("jdbcUrl"); user = properties. getProperty ("user"); password = properties. getProperty ("password"); // load the database Driver (register the driver) try {Class. forName (driverClass);} catch (ClassNotFoundException e) {e. printStackTrace ();} Connection connection = null; try {connection = DriverManager. getConnection (jdbcUrl, user, password);} catch (SQLException e) {e. printStackTrace ();} return connection ;}}
Then I wrote a general update method.
/*** Common UPDATE Methods: including INSERT/update/DELETE * @ param SQL */public void UPDATE (String SQL) {Connection connection = null; Statement statement = null; try {connection = JDBCTools. getConnection (); statement = connection. createStatement (); statement.exe cuteUpdate (SQL);} catch (Exception e) {e. printStackTrace ();} finally {JDBCTools. release (statement, connection );}}
I'm too lazy to write a test class. Believe me ~