日期:2014-05-17 浏览次数:21180 次
package preparedDemo;
//学习数据库很简单的,就只有四个步骤:1.加载驱动;2.链接数据库;3.操作数据;4.关闭数据库;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class PreExcuteUpdateJDBC {
//驱动程序就是之前在classpa中配置的jdbc的驱动程序的jar包中;
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
//链接地址是由各个数据库生产商单独提供的,所以需要单独记住
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:zhu";
public static final String DBUSER = "scott";//连接数据库用户
public static final String DBPASS = "tiger";//连接数据库密码
public static void main(String[] args)throws Exception{
Connection conn = null;//表示数据库的连接的对象
String sql = "INSERT INTO person(pid,name,age,birthdat,salary) VALUES(2,'ad',?,to_date('1992-01-06','YYYY-MM-DD'),4532.0) ";
Class.forName(DBDRIVER);//1.使用Class加载驱动程序
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);//2.连接数据库
PreparedStatement preState = conn.prepareStatement(sql);//3.操作数据
preState.setString(1, "asda");
printf(sql);
preState.executeUpdate(sql);
preState.close();
conn.close(); //4.关闭数据库;
}
public static void printf(Object obj){
System.out.println(obj);
}
}