SELECT操作
说明: 请先完成创建表和插入操作。
使用prepareStatement执行查询操作可以使用预编译,动态的添加参数也可以执行普通的SQL语句。
public static void main(String[] args) throws SQLException {
Connection conn = getConnect();
PreparedStatement preparedStatement=conn.prepareStatement("select * from test where id=?;");
preparedStatement.setObject(1,1);
ResultSet resultSet=preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getObject("id")+" "+
resultSet.getObject("name"));
}
conn.close();
}
public static Connection getConnect() {
String driver = "org.opengauss.Driver";
String sourceURL = "jdbc:opengauss://127.0.0.1:8080/test?user=myuser&password=myPassWord";
Properties info = new Properties();
Connection conn = null;
try {
Class.forName(driver);
} catch (Exception var9) {
var9.printStackTrace();
return null;
}
try {
conn = DriverManager.getConnection(sourceURL);
System.out.println("连接成功!");
return conn;
} catch (Exception var8) {
var8.printStackTrace();
return null;
}
}
使用Statement执行查询语句。
public static void main(String[] args) throws SQLException {
Connection conn = getConnect();
Statement statement = conn.createStatement();
ResultSet resultSet=statement.executeQuery("select * from test");
while (resultSet.next()){
System.out.println(resultSet.getObject("id")+" "+
resultSet.getObject("name"));
}
conn.close();
}
public static Connection getConnect() {
String driver = "org.opengauss.Driver";
String sourceURL = "jdbc:opengauss://127.0.0.1:8080/test?user=myuser&password=myPassWord";
Properties info = new Properties();
Connection conn = null;
try {
Class.forName(driver);
} catch (Exception var9) {
var9.printStackTrace();
return null;
}
try {
conn = DriverManager.getConnection(sourceURL);
System.out.println("连接成功!");
return conn;
} catch (Exception var8) {
var8.printStackTrace();
return null;
}
}
更新操作
说明: 请先完成创建表和插入操作。
使用Statement执行更新操作,然后执行查询语句判断更新是否成功。
public static void main(String[] args) throws SQLException {
Connection conn = getConnect();
Statement statement = conn.createStatement();
statement.execute("update test set name='wangwu' where id=1");
PreparedStatement preparedStatement=conn.prepareStatement("select * from test");
ResultSet resultSet=preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getObject("id")+" "+
resultSet.getObject("name"));
}
conn.close();
}
public static Connection getConnect() {
String driver = "org.opengauss.Driver";
String sourceURL = "jdbc:opengauss://127.0.0.1:8080/test?user=myuser&password=myPassWord";
Properties info = new Properties();
Connection conn = null;
try {
Class.forName(driver);
} catch (Exception var9) {
var9.printStackTrace();
return null;
}
try {
conn = DriverManager.getConnection(sourceURL);
System.out.println("连接成功!");
return conn;
} catch (Exception var8) {
var8.printStackTrace();
return null;
}
}
使用PreparedStatement执行更新操作,然后执行查询语句判断更新是否成功。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcConn {
public static void main(String[] args) throws SQLException {
Connection conn = getConnect();
Statement statement = conn.createStatement();
PreparedStatement preparedStatement=conn.prepareStatement("update test set name='wangwu' where id=1");
preparedStatement.execute();
preparedStatement=conn.prepareStatement("select * from test;");
ResultSet resultSet=preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getObject("id")+" "+
resultSet.getObject("name"));
}
conn.close();
}
public static Connection getConnect() {
String driver = "org.opengauss.Driver";
String sourceURL = "jdbc:opengauss://127.0.0.1:8080/test?user=myuser&password=myPassWord";
Properties info = new Properties();
Connection conn = null;
try {
Class.forName(driver);
} catch (Exception var9) {
var9.printStackTrace();
return null;
}
try {
conn = DriverManager.getConnection(sourceURL);
System.out.println("连接成功!");
return conn;
} catch (Exception var8) {
var8.printStackTrace();
return null;
}
}
}
删除操作
使用Statement进行删除,然后执行查询语句判断删除是否成功。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcConn {
public static void main(String[] args) throws SQLException {
Connection conn = getConnect();
Statement statement = conn.createStatement();
statement.execute("delete from test where id =4");
PreparedStatement preparedStatement=conn.prepareStatement("select * from test;");
ResultSet resultSet=preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getObject("id")+" "+
resultSet.getObject("name"));
}
conn.close();
}
public static Connection getConnect() {
String driver = "org.opengauss.Driver";
String sourceURL = "jdbc:opengauss://10.244.50.251:33700/test_l?user=ltest&password=tiange1999?";
Properties info = new Properties();
Connection conn = null;
try {
Class.forName(driver);
} catch (Exception var9) {
var9.printStackTrace();
return null;
}
try {
conn = DriverManager.getConnection(sourceURL);
System.out.println("连接成功!");
return conn;
} catch (Exception var8) {
var8.printStackTrace();
return null;
}
}
}
使用preparedStatement进行删除,然后执行查询语句判断删除是否成功。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcConn {
public static void main(String[] args) throws SQLException {
Connection conn = getConnect();
Statement statement = conn.createStatement();
PreparedStatement preparedStatement=conn.prepareStatement("delete from test where id =2");
preparedStatement.execute();
preparedStatement=conn.prepareStatement("select * from test;");
ResultSet resultSet=preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getObject("id")+" "+
resultSet.getObject("name"));
}
conn.close();
}
public static Connection getConnect() {
String driver = "org.opengauss.Driver";
String sourceURL = "jdbc:opengauss://10.244.50.251:33700/test_l?user=ltest&password=tiange1999?";
Properties info = new Properties();
Connection conn = null;
try {
Class.forName(driver);
} catch (Exception var9) {
var9.printStackTrace();
return null;
}
try {
conn = DriverManager.getConnection(sourceURL);
System.out.println("连接成功!");
return conn;
} catch (Exception var8) {
var8.printStackTrace();
return null;
}
}
}