您的位置:首页 > 其它

JDBC初步

2014-10-23 01:07 337 查看
JDBC

import java.sql.*;

public class TestJDBC {
public static void main(String[] args) {
ResultSet rs = null;
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/world";
conn = DriverManager.getConnection(url, "hill", "hill");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from city");
while (rs.next()) {
System.out.println(rs.getString("Name"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

}

}


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



可滚动的结果集

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: