import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class WorkerDao { /**
* @param args
*/
static Connection con;
static Statement st;
static ResultSet rs;
//连接
public void connectDB(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL",
"test","test");
st=con.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//插入
public void insertData(String name,int age,String statement){
this.connectDB();
try {
String sql="insert into test.worker(name,age,statement) values ('"+name+"',"+age+",'"+statement+"')";
rs=st.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeConnection();
}
}
//删除
public void deleteData(int id){
this.connectDB();
try {
rs=st.executeQuery("delete from worker w where w.id="+id+";commit;");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeConnection();
}
}
//修改
public void updateData(int id,String name,int age,String statement){
this.connectDB();
try {
rs=st.executeQuery("update worker w set name='"+name+"',age='"+age+"',statement='"+statement+"' where w.id="+id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeConnection();
}
}
//查询
public void selectData(){
this.connectDB();
try {
rs=st.executeQuery("select * from worker t");
while(rs.next()){
String name=rs.getString(1);
int age=rs.getInt(2);
String statement=rs.getString(3);
System.out.println(name+","+age+","+statement);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeConnection();
}
}
public void closeConnection(){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {//测试类
WorkerDao wd=new WorkerDao();
//查询(成功)
wd.selectData();
//增加(成功)
wd.insertData("张大大", 22, "试一试");
//删除(失败:无效字符??)
wd.deleteData(4);
//更新(失败:无效ID)
wd.updateData(1, "ddd", 45, "dasdasd");
}
}
求解!!!!
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class WorkerDao { /**
* @param args
*/
static Connection con;
static Statement st;
static ResultSet rs;
//连接
public void connectDB(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL",
"test","test");
st=con.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//插入
public void insertData(String name,int age,String statement){
this.connectDB();
try {
String sql="insert into test.worker(name,age,statement) values ('"+name+"',"+age+",'"+statement+"')";
rs=st.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeConnection();
}
}
//删除
public void deleteData(int id){
this.connectDB();
try {
rs=st.executeQuery("delete from worker w where w.id="+id+";commit;");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeConnection();
}
}
//修改
public void updateData(int id,String name,int age,String statement){
this.connectDB();
try {
rs=st.executeQuery("update worker w set name='"+name+"',age='"+age+"',statement='"+statement+"' where w.id="+id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeConnection();
}
}
//查询
public void selectData(){
this.connectDB();
try {
rs=st.executeQuery("select * from worker t");
while(rs.next()){
String name=rs.getString(1);
int age=rs.getInt(2);
String statement=rs.getString(3);
System.out.println(name+","+age+","+statement);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeConnection();
}
}
public void closeConnection(){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {//测试类
WorkerDao wd=new WorkerDao();
//查询(成功)
wd.selectData();
//增加(成功)
wd.insertData("张大大", 22, "试一试");
//删除(失败:无效字符??)
wd.deleteData(4);
//更新(失败:无效ID)
wd.updateData(1, "ddd", 45, "dasdasd");
}
}
求解!!!!
解决方案 »
- PL/SQL 菜鸟提问 怎么把相同的人的记录放到同一行中,航班号用逗号隔开
- SQL语句问题,请高手解答
- Oracle 11g For Linux x86在Red Hat Enterprise Linux AS 5.0上的详细安装过程(图解)
- 逗号分隔字段取前三项
- oracle磁盘空间不足怎么办
- 触发器问题。没有数据?
- oracle9i安装程序三个文件下载完后,解压缩成三个*.cpio文件,应该再怎么办?
- 提示的前景色和背景色的设置
- 在查询表时如何把同列的重复数据只列出一条,在线等待,急急急急
- ***!!求教SQL简单问题!关于通配符的使用!100分求教,在线等待!
- Oracle SQL 重名中文 ORA-00911: invalid character
- GoldenGate 注册 crs 资源后,启动出错
1、rs=st.executeQuery("delete from worker w where w.id="+id+";commit;");
改为conn.commit()
查询用executeQuery()
插入和修改用executeUpdate()