1、导入jar包
https://jdbc.postgresql.org/download.html
在这个里面去下载你所需要的jar包;直接复制粘贴到eclipse里的WEB-INF下的lib里面。
2、连接数据库
package collection;
import java.sql.Connection;
import java.sql.DriverManager;
public class PostgreSQLJDBC {
public static void main(String args[]) {
//连接
Connection c = null;
try {
//加载数据驱动,数据库
Class.forName("org.postgresql.Driver");
//连接数据库地址、名称、密码
c = DriverManager
.getConnection("jdbc:postgresql://127.0.0.1:5432/demo",
"postgres", "xxx");
} catch (Exception e) {
e.printStackTrace();
//err是运行期异常和错误反馈的输出流的方向
System.err.println(e.getClass().getName()+": "+e.getMessage());
//调用exit(0)方法终止虚拟机也就是退出你的Java程序,括号里面的是参数,进程结束的返回值。
System.exit(0);
}
//输出 打开数据库成功
System.out.println("Opened database successfully");
}
}
3、创建数据库
package collection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
* 创建表
* @author Administrator
*
*/
public class CreateTable {
public static void main(String[] args) throws Exception {
//连接数据库
Connection c = null;
//将 SQL 语句发送到数据库中
Statement stmt = null;
try {
//加载数据驱动,数据库
Class.forName("org.postgresql.Driver");
//地址,用户名,密码
c = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/demo", "postgres", "xxx");
System.out.println("Opened database successfully");
//Statement stmt = conn.createStatement();
//将 SQL 语句发送到数据库中
stmt = c.createStatement();
//sql语句的输入
String sql = "CREATE TABLE BBQ " +
"(ID INT PRIMARY KEY NOT NULL," +
" NAME TEXT NOT NULL, " +
" AGE INT NOT NULL, " +
" ADDRESS CHAR(50), " +
" SALARY REAL)";
stmt.executeUpdate(sql);
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Table created successfully");
}
}
4、插入数据库
package collection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class InsertTable {
public static void main(String[] args) throws Exception {
Connection c = null;
Statement stmt = null;
try {
//加载数据驱动,数据库
Class.forName("org.postgresql.Driver");
//连接数据库地址、名称、密码
c = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/demo", "postgres", "xxx");
//当我们进行多条数据进行增删改的时候,一旦在一句sql中出现了错误,
//就会出现有部分数据已经成功,而后面的数据就没有办法执行,这个时候,就会出现脏数据。
//因此我们使用setAutoCommit方法,这个方法有一个参数,参数值为Boolean,
//当true的时候可启用自动提交模式,false可禁用该模式。
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql =
"INSERT INTO BBQ(ID,NAME,AGE,ADDRESS)"
+ "VALUES(1,'CXM',22,'bj');";
stmt.executeUpdate(sql);
sql = "INSERT INTO BBQ (ID,NAME,AGE,ADDRESS) "
+ "VALUES (2, 'Allen', 25, 'Texas');";
stmt.executeUpdate(sql);
sql = "INSERT INTO BBQ (ID,NAME,AGE,ADDRESS) "
+ "VALUES (3, 'Teddy', 23, 'Norway');";
stmt.executeUpdate(sql);
sql = "INSERT INTO BBQ (ID,NAME,AGE,ADDRESS) "
+ "VALUES (4, 'Mark', 25, 'Rich-Mond ');";
stmt.executeUpdate(sql);
stmt.close();
c.commit();
c.close();
} catch (ClassNotFoundException e) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Records created successfully");
}
}
5、更新数据库
package collection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateTable {
public static void main(String[] args) throws Exception {
Connection c = null;
Statement stat = null;
try {
//加载数据驱动,数据库
Class.forName("org.postgresql.Driver");
//连接数据库地址、名称、密码
c= DriverManager
.getConnection("jdbc:postgresql://127.0.0.1:5432/demo", "postgres", "xxx");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stat = c.createStatement();
String sql = "UPDATE BBQ SET AGE=15 WHERE ID=2 ;";
stat.executeUpdate(sql);
c.commit();
//查询数据库
ResultSet rs = stat.executeQuery("SELECT * FROM BBQ;");
while ( rs.next() ) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String address = rs.getString("address");
System.out.println( "ID = " + id );
System.out.println( "NAME = " + name );
System.out.println( "AGE = " + age );
System.out.println( "ADDRESS = " + address );
System.out.println();
}
rs.close();
stat.close();
c.close();
} catch (ClassNotFoundException e) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
}
6、删除数据库
package collection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DeleteTable {
public static void main( String args[] )
{
//连接数据库
Connection c = null;
//将 SQL 语句发送到数据库中
Statement stmt = null;
try {
//加载数据驱动,数据库
Class.forName("org.postgresql.Driver");
//连接数据库地址、名称、密码
c = DriverManager
.getConnection("jdbc:postgresql://127.0.0.1:5432/demo",
"postgres", "xxx");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql = "DELETE from COMPANY where ID=2;";
stmt.executeUpdate(sql);
c.commit();
//查询数据库表
ResultSet rs = stmt.executeQuery( "SELECT * FROM BBQ;" );
while ( rs.next() ) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String address = rs.getString("address");
System.out.println( "ID = " + id );
System.out.println( "NAME = " + name );
System.out.println( "AGE = " + age );
System.out.println( "ADDRESS = " + address );
System.out.println();
}
//关闭
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
}
有参考其他的资料!!