Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.Statement; public class Test {/** * @param args */public static void main (string[] args) {/* This part is to connect the Oracle database with Java driver, drive Jar can be downloaded on the Internet itself, you can also give me a message, send you * */try {//Operation Database First step: Find the driver Class.forName ("Oracle.jdbc.driver.OracleDr Iver "); Step two: Establish a connection Connection con = drivermanager.getconnection ("Jdbc:oracle:thin:@127.0.0.1:1521:oracle", "David", "110" ); 127.0.0.1 is the database host that needs to connect the ip,oracle is the database name, David is the login name, 110 is the password, the other parts are fixed unchanged! Step three: Establish a Statement object Statement st = Con.createstatement (); Fourth step: Write out the SQL statement and execute the Statement object method//String sql = "Update person set pname = ' ling ' where pname = ' ling '";// St.executeupdate (SQL); Additions and deletions are used this execution of the statement because of the update database String sql = "SELECT * from the person"; ResultSet rs=st.executequery (SQL); Query with this executive languageThe result of the sentence query is saved in the result set ResultSet class object Rs in while (Rs.next ()) { Rs.next () returns true if the result set RS has the next record, otherwise returns false int id = rs.getint ("PersonID"); String name = rs.getstring (2); String sex = rs.getstring (3); System.out.println (id+ "--->" +name+ "--------" +sex); } con.close (); Remember to close the connection after the database operation} catch (Exception e) {e.printstacktrace (); }
JDBC Java database connection with simple SQL statement execution