import java.sql.*; public class TestJDBC { public static void main(String[] args) { ResultSet rs = null; Statement stmt = null; Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root"); stmt = conn.createStatement(); rs = stmt.executeQuery("select * from dept"); while (rs.next()) { System.out.println(rs.getString("deptno")); System.out.println(rs.getInt("deptno")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); // log4j 啊!你赶紧过来吧,阿里需要你!。 } finally { try { if (rs != null) { rs.close(); rs = null; // 置为空吧,垃圾回收器会回收的。 } if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } }向数据库中插入一条语句
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestDML { public static void main(String[] args) { Statement stmt = null; Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root"); stmt = conn.createStatement(); String sql = "insert into dept2 values (98, 'GAME', 'BJ')"; stmt.executeUpdate(sql); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } }
DML(Data Manipulation Language)数据操纵语言命令使用户能够查询数据库以及操作已有数据库中的数据。基本的数据操作分成两类四种:检索(查询)和更新(插入、删除、修改)。DML分成交互型DML和嵌入型DML两类。依据语言的级别,DML又可分成过程性DML和非过程性DML两种。如insert,delete,update,select(插入、删除、修改、检索)等都是DML.
命令行传入参数,执行 DML
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestDML2 { public static void main(String[] args) { if(args.length != 3) { System.out.println("Parameter Error! Please Input Again!"); System.exit(-1); } int deptno = 0; try { deptno = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.out.println("Parameter Error! Deptno should be Number Format!"); System.exit(-1); } String dname = args[1]; String loc = args[2]; Statement stmt = null; Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root"); stmt = conn.createStatement(); String sql = "insert into dept2 values (" + deptno + ",'" + dname + "','" + loc + "')"; System.out.println(sql); stmt.executeUpdate(sql); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if(stmt != null) { stmt.close(); stmt = null; } if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } }