This is a complete database operation short program;
Package example; import java. io. fileInputStream; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. resultSetMetaData; import java. SQL. statement; import java. util. properties; public class ExecuteSQL {/*** @ param args */private String driver; private String url; private String user; private String pass; Connection conn; Statement stmt; ResultSet rs; public Void initParam (String paramFile) throws Exception {Properties props = new Properties (); props. load (new FileInputStream (paramFile); driver = props. getProperty ("driver"); url = props. getProperty ("url"); user = props. getProperty ("user"); pass = props. getProperty ("pass");} public void executeSql (String SQL) throws Exception {try {// load driver Class. forName (driver); // obtain the database connection conn = DriverManager. getConnectio N (url, user, pass); // use Connection to create a Statement object stmt = conn. createStatement (); // run the SQL statement. The returned boolean value indicates whether the ResultSetboolean hasResultSet = stmt.exe cute (SQL); if (hasResultSet) {// obtain the result set rs = stmt. getResultSet (); // ResultSetMetaData is the metadata interface used to analyze the result set. ResultSetMetaData rsmd = rs. getMetaData (); int columnCount = rsmd. getColumnCount (); while (rs. next () {for (int I = 0; I <columnCount; I ++) {System. out. println (rs. GetString (I + 1) + "\ t");} System. out. println ("\ n") ;}} else {System. out. println ("records affected by the SQL statement" + stmt. getUpdateCount () + "bar") ;}} finally {if (rs! = Null) {rs. close () ;}if (stmt! = Null) {stmt. close ();} if (conn! = Null) {conn. close () ;}} public static void main (String [] args) throws Exception {// TODO Auto-generated method stubExecuteSQL es = new ExecuteSQL (); es. initParam ("d: \ mysql. ini "); System. out. println ("------- execute the DDL statement -------") eses.exe cuteSql ("drop table if exists my_test"); System. out. println ("------- execute the table creation DDL statement ---------") eses.exe cuteSql ("create table my_test" + "(test_id int auto_increment primary key," + "test_name varchar (255 )) "); System. out. println ("------ execute the DML statement for inserting data ------") eses.exe cuteSql ("insert into my_test (test_name)" + "select student_name from student_table2"); System. out. println ("----- execute the data query statement ------") eses.exe cuteSql ("select * from my_test ");}}
D: \ mysql. ini creates a MySQL. ini configuration file on the disk. The content of the configuration file is:
Driver = com. MySQL. JDBC. Driver
Url = JDBC: mysql: // 127.0.0.1: 3306/homework
User = root
Pass = root
Note that each row cannot be followed by Spaces
This is the biggest achievement of the day. Today, it is still not ideal to laugh. Many times I am chatting about QQ, turning web pages, or playing games that match each other. I will stick to my credibility tomorrow, you can no longer be confused ~~