如Insert,Update等操作不返回结果集,使用Statement的executeUpdate(String sql)方法
以下为搜索的操作数据库的例子:
import java.lang.*;
import java.util.*;
import java.sql.*;
public class db {
public static void main(String argv[]) {
System.out.println("start...");
//init database connection
Connection pdb;
try
{
Class.forName("postgresql.Driver");
}
catch(java.lang.ClassNotFoundException e) {
System.out.println("err:class.forname.");
}
try
{
pdb=DriverManager.getConnection("jdbc
ostgresql:mytest","eight","");
Statement stmt=pdb.createStatement();
ResultSet rs=stmt.executeQuery("select * from test1");
if(rs!=null)
{
System.out.println("get data from database:");
while(rs.next())
{
System.out.print(rs.getString(1));
System.out.print(rs.getint(2));
System.out.print("");
}
}
rs.close();
stmt.close();
pdb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}