标签:style blog http color io os ar java for
JDBC
1 import java.sql.*; 2 3 public class TestJDBC { 4 public static void main(String[] args) { 5 ResultSet rs = null; 6 Statement stmt = null; 7 Connection conn = null; 8 try { 9 // Class是java.lang的包, 给名子把类的实例new出来 可能找不到类 抛异常ClassNotFoundException 10 // 自动向drivermanager注册 11 Class.forName("com.mysql.jdbc.Driver"); 12 String url = "jdbc:mysql://127.0.0.1:3307/world"; 13 conn = DriverManager.getConnection(url, "hill", "hill"); 14 stmt = conn.createStatement(); 15 rs = stmt.executeQuery("select * from city"); 16 while (rs.next()) { 17 System.out.println(rs.getString("Name")); 18 } 19 } catch (ClassNotFoundException e) { 20 e.printStackTrace(); 21 } catch (SQLException e) { 22 e.printStackTrace(); 23 } finally { 24 try { 25 if (rs != null) { 26 rs.close(); 27 } 28 if (stmt != null) { 29 stmt.close(); 30 } 31 if (conn != null) { 32 conn.close(); 33 } 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 } 37 } 38 39 } 40 41 }
DML1
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是java.lang的包, 给名子把类的实例new出来 可能找不到类 抛异常ClassNotFoundException
// 自动向drivermanager注册
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3307/samp_db";
conn = DriverManager.getConnection(url, "hill", "hill");
stmt = conn.createStatement();
String sql = "insert into tab_user values (02,‘hehe‘)";
stmt.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
DML2
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 != 2) {
System.out.println("Parameter Error!");
System.exit(-1);
}
int num = 0;
try {
num = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.out.println("Parameter Error! NumberFormatException!");
System.exit(-1);
}
String name = args[1];
Statement stmt = null;
Connection conn = null;
try {
// Class是java.lang的包, 给名子把类的实例new出来 可能找不到类 抛异常ClassNotFoundException
// 自动向drivermanager注册
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3307/samp_db";
conn = DriverManager.getConnection(url, "hill", "hill");
stmt = conn.createStatement();
String sql = "insert into tab_user values " + "(" + num + ",‘"
+ name + "‘)";
System.out.println(sql);
stmt.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
PreparedStatement
import java.sql.*;
public class TestPrepStmt {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Parameter Error!");
System.exit(-1);
}
int num = 0;
try {
num = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.out.println("Parameter Error! NumberFormatException!");
System.exit(-1);
}
String name = args[1];
PreparedStatement pstmt = null;
Connection conn = null;
try {
// Class是java.lang的包, 给名子把类的实例new出来 可能找不到类 抛异常ClassNotFoundException
// 自动向drivermanager注册
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3307/samp_db";
conn = DriverManager.getConnection(url, "hill", "hill");
pstmt = conn.prepareStatement("inset into tab_user values(?,?,?)");
pstmt.setInt(1,num);
pstmt.setString(2,name);
pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
批处理


Transaction

可滚动的结果集

标签:style blog http color io os ar java for
原文地址:http://www.cnblogs.com/tianc-y/p/4044795.html