步骤
向容器之中进行数据库驱动的加载:Class.forName(数据库驱动程序);
通过DriverManager取得一个连接对象:DriverManager.getConncetion();
通过连接对象创建所有的数据库操作对象,并进行数据库的更新查询;
数据库属于资源操作资源操作的结束必须关闭colse();
//连接数据库
public class HelloServer {
public static final String DBDRIVER = "oracle.jdbc.dirver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@loaclhost:1521:mldn";
public static final String DBUSER = "scott";
public static final String DBPASSWORD = "tiger";
public static void main(String[] args) throws Exception {
Class.forName(DBDRIVER);
Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
conn.close();
}
}
Statement
//更新数据库
public class HelloServer {
public static final String DBDRIVER = "oracle.jdbc.dirver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@loaclhost:1521:mldn";
public static final String DBUSER = "scott";
public static final String DBPASSWORD = "tiger";
public static void main(String[] args) throws Exception {
Class.forName(DBDRIVER);
Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
Statement stmt = conn.createStatement();
String sql=" INSERT INTO MAMBER(mid,name,age,birthday,note) VALUES "
+" mysql.nextval,'张三',10,SYSDATE,'人' ";
int len = stmt.executeUpdate(sql);
System.out.println("数据库更新行数:"+len);
stmt.close();
conn.close();
}
}
//数据库查询
public class HelloServer {
public static final String DBDRIVER = "oracle.jdbc.dirver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@loaclhost:1521:mldn";
public static final String DBUSER = "scott";
public static final String DBPASSWORD = "tiger";
public static void main(String[] args) throws Exception {
Class.forName(DBDRIVER);
Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
Statement stmt = conn.createStatement();
String sql=" SELECT mid,nsme,age,birthday,note FROM member ";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int mid = rs.getInt("mid");
String name =rs.getString("name");
int age = rs.getInt(3);
Date birthday = rs.getDate(4);
String note = rs.getString(5);
System.out.println(mid+"、"+name+"、"+age+"、"+birthday+"、"+note);
}
rs.close();
stmt.close();
conn.close();
}
}
PreparedStatement:
//更新数据库
public class HelloServer {
public static final String DBDRIVER = "oracle.jdbc.dirver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@loaclhost:1521:mldn";
public static final String DBUSER = "scott";
public static final String DBPASSWORD = "tiger";
public static void main(String[] args) throws Exception {
String name = "Mr'Smith";
int age = 18;
Date birthday =new Date();
String note = "是个人";
Class.forName(DBDRIVER);
Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
String sql=" INSERT INTO MAMBER(mid,name,age,birthday,note) VALUES "
+" (myseq.nextval,?,?,?,?)";
PreparedStatement pstmt =conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setInt(2, age);
pstmt.setDate(3, new java.sql.Date(birthday.getTime()));
pstmt.setString(4, note);
System.out.println("数据库更新操作:"+pstmt.executeQuery());
pstmt.close();
conn.close();
}
}
//查询所有数据
public class HelloServer {
public static final String DBDRIVER = "oracle.jdbc.dirver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@loaclhost:1521:mldn";
public static final String DBUSER = "scott";
public static final String DBPASSWORD = "tiger";
public static void main(String[] args) throws Exception {
Class.forName(DBDRIVER);
Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
String sql=" SELECT mid,name,age,birthday,note FROM member ";
PreparedStatement pstmt =conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
int mid = rs.getInt("mid");
String name =rs.getString("name");
int age = rs.getInt(3);
Date birthday = rs.getDate(4);
String note = rs.getString(5);
System.out.println(mid+"、"+name+"、"+age+"、"+birthday+"、"+note);
}
rs.close();
pstmt.close();
conn.close();
}
}
网友评论