数据库查询的几种方法
2007-05-05 23:45
295 查看
1、Statement接口查询
这通常是比较常用的。
Statement statement = connection.createStatement();
ResultSet rs = getResultSet("SELECT * FROM TB_STUDENTS WHERE AGE=20");
...
while (rs.next())
...{
...
}
2、PreparedStatement接口查询
如果需要多次执行SQL语句,那么PreparedStatement是首选。因为他包含了一个已经被编译的SQL语句,提高了程序的效率和性能。
下面分别对TB_CUSTOMERS表,进行查询、插入、修改、删除的例子。
查询:
public void preQueryCustomersTB(int ID)
...{
try
...{
PreparedStatement pstmt = connection.prepareStatement("SELECT * FROM TB_CUSTOMERS WHERE ID = ?;");
pstmt.setInt(1, ID);
pstmt.executeQuery();
connection.commit();
pstmt.clearParameters();
} catch (SQLException e)
...{
e.printStackTrace();
}
}
插入:
public boolean preInsertCustomersTB(String company,String name,String address,String email,String phone,String other)
...{
try
...{
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO TB_CUSTOMERS (COMPANY,NAME,ADDRESS,EMAIL,PHONE,OTHER) VALUES (?,?,?,?,?,?) ;");
pstmt.setString(1, company);
pstmt.setString(2, name);
pstmt.setString(3, address);
pstmt.setString(4, email);
pstmt.setString(5, phone);
pstmt.setString(6, other);
pstmt.execute();
connection.commit();
} catch (SQLException e)
...{
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
}
return true;
}
修改:
public boolean preUpdataCustomersTB(int id,String company,String name,String address,String email,String phone,String other)
...{
try
...{
PreparedStatement pstmt = connection.prepareStatement(
"update TB_CUSTOMERS SET company = ?, name = ?, address = ?, email = ?, phone = ?, other = ? WHERE ID = ?;");
pstmt.setString(1, company);
pstmt.setString(2, name);
pstmt.setString(3, address);
pstmt.setString(4, email);
pstmt.setString(5, phone);
pstmt.setString(6, other);
pstmt.setInt(7, id);
pstmt.execute();
connection.commit();
} catch (SQLException e)
...{
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
}
return true;
}
删除:
public void preDeletCustomersTB(int RowID)
...{
try
...{
PreparedStatement pstmt = connection.prepareStatement("delete FROM TB_CUSTOMERS where ID = ?;");
pstmt.setInt(1, RowID);
pstmt.execute();
connection.commit();
} catch (SQLException e)
...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
3、CallableStatement接口查询
CallableStatement接口提供了通过JDBC API调用SQL存储过程的标准途径。
使用IN参数的例子:
public void callSetPlayer(String player)
...{
try
...{
CallableStatement cStatement = connection.prepareCall("{CALL setPlayName(?}");
cStatement.setString(1, "John Doe");
cStatement.execute();
} catch (SQLException e)
...{
e.printStackTrace();
}
}
使用OUT参数的例子:
public String callGetPlayer()
...{
try
...{
CallableStatement cStatement = connection.prepareCall("{CALL getPlayName(?}");
cStatement.registerOutParameter(1, java.sql.Types.VARCHAR);
cStatement.execute();
String name = cStatement.getString(1);
//...
return name;
} catch (SQLException e)
...{
e.printStackTrace();
return null;
}
}
这通常是比较常用的。
Statement statement = connection.createStatement();
ResultSet rs = getResultSet("SELECT * FROM TB_STUDENTS WHERE AGE=20");
...
while (rs.next())
...{
...
}
2、PreparedStatement接口查询
如果需要多次执行SQL语句,那么PreparedStatement是首选。因为他包含了一个已经被编译的SQL语句,提高了程序的效率和性能。
下面分别对TB_CUSTOMERS表,进行查询、插入、修改、删除的例子。
查询:
public void preQueryCustomersTB(int ID)
...{
try
...{
PreparedStatement pstmt = connection.prepareStatement("SELECT * FROM TB_CUSTOMERS WHERE ID = ?;");
pstmt.setInt(1, ID);
pstmt.executeQuery();
connection.commit();
pstmt.clearParameters();
} catch (SQLException e)
...{
e.printStackTrace();
}
}
插入:
public boolean preInsertCustomersTB(String company,String name,String address,String email,String phone,String other)
...{
try
...{
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO TB_CUSTOMERS (COMPANY,NAME,ADDRESS,EMAIL,PHONE,OTHER) VALUES (?,?,?,?,?,?) ;");
pstmt.setString(1, company);
pstmt.setString(2, name);
pstmt.setString(3, address);
pstmt.setString(4, email);
pstmt.setString(5, phone);
pstmt.setString(6, other);
pstmt.execute();
connection.commit();
} catch (SQLException e)
...{
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
}
return true;
}
修改:
public boolean preUpdataCustomersTB(int id,String company,String name,String address,String email,String phone,String other)
...{
try
...{
PreparedStatement pstmt = connection.prepareStatement(
"update TB_CUSTOMERS SET company = ?, name = ?, address = ?, email = ?, phone = ?, other = ? WHERE ID = ?;");
pstmt.setString(1, company);
pstmt.setString(2, name);
pstmt.setString(3, address);
pstmt.setString(4, email);
pstmt.setString(5, phone);
pstmt.setString(6, other);
pstmt.setInt(7, id);
pstmt.execute();
connection.commit();
} catch (SQLException e)
...{
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
}
return true;
}
删除:
public void preDeletCustomersTB(int RowID)
...{
try
...{
PreparedStatement pstmt = connection.prepareStatement("delete FROM TB_CUSTOMERS where ID = ?;");
pstmt.setInt(1, RowID);
pstmt.execute();
connection.commit();
} catch (SQLException e)
...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
3、CallableStatement接口查询
CallableStatement接口提供了通过JDBC API调用SQL存储过程的标准途径。
使用IN参数的例子:
public void callSetPlayer(String player)
...{
try
...{
CallableStatement cStatement = connection.prepareCall("{CALL setPlayName(?}");
cStatement.setString(1, "John Doe");
cStatement.execute();
} catch (SQLException e)
...{
e.printStackTrace();
}
}
使用OUT参数的例子:
public String callGetPlayer()
...{
try
...{
CallableStatement cStatement = connection.prepareCall("{CALL getPlayName(?}");
cStatement.registerOutParameter(1, java.sql.Types.VARCHAR);
cStatement.execute();
String name = cStatement.getString(1);
//...
return name;
} catch (SQLException e)
...{
e.printStackTrace();
return null;
}
}
相关文章推荐
- 查询数据库,取的数据的几种方法
- Hibernate中查询数据库的几种方法
- MS数据库优化查询最常见的几种方法
- 【mysql】监控数据库和抓取慢查询的几种方法汇总
- 几种数据库查询前10条记录的方法
- 数据库多表查询的几种方法
- 数据库多表查询的几种方法
- DELPHI设计数据库查询的几种方法
- xx并发压测xx系统注册、查询、绑定邮箱等业务,数据库连接报异常及解决方法
- 查询整个数据库中某个特定值所在的表和字段的方法
- 数据库软件安装和数据库创建的几种方法
- 简单数据库查询通用方法
- 数据库优化查询计划的方法
- 对Oralce数据库存取图片的几种方法
- 查询数据库中重复记录的方法
- sql查询数据库的常用优化方法
- 将dataTable一次性插入数据库的几种方法(SqlBulkCopy或存储过程)
- 数据库优化查询计划的方法
- mysql导出数据库几种方法
- Oracle、Vertica和Hive三种数据库查询表空间的方法