public class Information {
String id="";
String name="";
String advice="";
public String getAdvice() {
return advice;
}
public void setAdvice(String advice) {
this.advice = advice;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class JdbcTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.9.29:1521:ac","bre","123");
//用PreparedStatement实现,每次执行一个条件
//插入
PreparedStatement pstmt0 =conn.prepareStatement("INSERT INTO Information VALUES
(?, ?, ?)");
pstmt0.setString(1, "111");
pstmt0.setString(2, "周鑫");
pstmt0.setString(3, "开心");
int rtn0=pstmt0.executeUpdate();
//更改
PreparedStatement pstmt1 =conn.prepareStatement("UPDATE INFORMATION SET ADVICE=?WHERE
NAME=?");
pstmt1.setString(1, "xxxx");
pstmt1.setString(2, "aaaaa");
int rtn1=pstmt1.executeUpdate();
//删除
PreparedStatement pstmt2 =conn.prepareStatement("DELETE INFORMATION WHERE NAME=?");
pstmt2.setString(1, "aaaaa");
int rtn2=pstmt2.executeUpdate();
//查询
PreparedStatement pstmt3 =conn.prepareStatement("SELECT * FROM INFORMATION");
int rtn3=pstmt3.executeUpdate();
ResultSet rset1 =pstmt3.getResultSet();
while(rset1.next())
{
String s1=rset1.getString(1);
String s2=rset1.getString(2);
String s3=rset1.getString(3);
System.out.println(s1+"\t"+s2+"\t"+s3);
}
rset1.close();
pstmt3.close();
pstmt3.close();
pstmt0.close();
conn.close();
}
}