JDBC处理DML(SXT)
Demo1
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import com.sun.org.apache.xalan.internal.xsltc.runtime.Parameter;
- public class TestDML
- {
- /**
- * @JDBC处理DML。接收手动输入的参数
- */
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- if (args.length != 4)
- {
- System.out.println("Error,plesase input again!");
- System.exit(-1);
- }
- int userid = 0;
- try
- {
- userid = Integer.parseInt(args[0]);
- } catch (NumberFormatException e)
- {
- System.out.println("Number error");
- System.exit(-1);
- }
- String usernameString = args[1];
- String passwordString = args[2];
- String userflaString = args[3];
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/college";
- String user = "root";
- String password = "123456";
- conn = DriverManager.getConnection(url, user, password);
- stmt = conn.createStatement();
- String sql = "insert into user (userId,userName,userPassword,userFlag)values(" + userid+",'"+usernameString+"','"+passwordString+"','" +userflaString+ "')";
- stmt.executeUpdate(sql);
- System.out.println(sql);
- rs = stmt.executeQuery("select * from user where userName='testDML4'");
- while (rs.next())
- {
- System.out.print("username:");
- System.out.println(rs.getString("userName"));
- System.out.print("password:");
- System.out.println(rs.getString("userPassword"));
- }
- } catch (ClassNotFoundException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally
- {
- try
- {
- if (rs != null)
- {
- rs.close();
- rs = null;
- }
- if (stmt != null)
- {
- stmt.close();
- stmt.close();
- }
- if (conn != null)
- {
- conn.close();
- conn = null;
- }
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
Demo2
- import java.sql.*;
- /**
- * JDBC处理DML2。无输入参数。
- */
- public class TestJDBCDML
- {
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/college";
- String user = "root";
- String password = "123456";
- conn = DriverManager.getConnection(url, user, password);
- stmt = conn.createStatement();
- String sql = "insert into user (userName,userPassword,userFlag)values('testDML2','123','dml')";
- stmt.executeUpdate(sql);
- rs = stmt.executeQuery("select * from user where userName='testDML2'");
- while(rs.next())
- {
- System.out.print("username:");
- System.out.println(rs.getString("userName"));
- System.out.print("password:");
- System.out.println(rs.getString("userPassword"));
- }
- } catch (ClassNotFoundException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally
- {
- try
- {
- if(rs != null)
- {
- rs.close();
- rs = null;
- }
- if(stmt != null)
- {
- stmt.close();
- stmt.close();
- }
- if (conn != null)
- {
- conn.close();
- conn = null;
- }
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }