一般:
Test.java
package day01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) {
Jdbc();
}
public static void Jdbc(){
Connection conn=null;
java.sql.Statement st=null;
ResultSet rs=null;
PreparedStatement pst=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}try{
conn=DriverManager.getConnection("jdbc:oracle:thin:@OWEYOJ5DU7AAHZZ:1521:ORCL", "jbit", "bdqn");
//Statement的使用
st=conn.createStatement();
String sql="select * from user1";
rs=st.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getInt(1)+" ,"+rs.getString(2));
}
//PreparedStatement的使用
String sql1="insert into user1(id,name) values(?,?)";
pst=conn.prepareStatement(sql1);
pst.setInt(1, 2);
pst.setString(2, "niini");
pst.execute();
}catch(SQLException e){
e.printStackTrace();
}finally{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
封装
JdbcTest.java
package day01;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcTest {
public static void main(String[] args){
Connection conn=null;
Statement st=null;
ResultSet rs=null;
/*
JdbcUnit.Driver();
conn=JdbcUnit.conn();
*/
//调用单例模式
JdbcUnitSing.getJdbcUnitSing().Driver();
conn=JdbcUnitSing.getJdbcUnitSing().conn();
try {
st=conn.createStatement();
String sql="select * from user1";
rs=st.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getInt("id")+","+rs.getString("name"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JdbcUnit.closeConn(conn, st, rs);
}
}
用static 封装
jdbcUnit.java
package day01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class JdbcUnit {
private static String url="jdbc:oracle:thin:@OWEYOJ5DU7AAHZZ:1521:ORCL";
private static String name="jbit";
private static String password="bdqn";
private static Connection conn=null;
//实例化一次
private JdbcUnit(){
}
public static void Driver(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection conn(){
try {
return conn=DriverManager.getConnection(url,name,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void closeConn(Connection conn,Statement st,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
单例模式封装
JdbcUintSing.java
package day01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class JdbcUnitSing {
private static String url="jdbc:oracle:thin:@OWEYOJ5DU7AAHZZ:1521:ORCL";
private static String name="jbit";
private static String password="bdqn";
private static Connection conn=null;
//实例化一次
private JdbcUnitSing(){
}
//只创建一个实例
private static JdbcUnitSing jus = new JdbcUnitSing();
//给外界提供实例的访问对象
public static JdbcUnitSing getJdbcUnitSing(){
return jus;
}
public void Driver(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public Connection conn(){
try {
return conn=DriverManager.getConnection(url,name,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void closeConn(Connection conn,Statement st,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}