Use of JDBC
Package com. jack. jdbc; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; // JDBC case public class JDBCTest {// use JDBC // first of all: the jar package/orcal is introduced in the app \ Administrator \ product \ 11.1.0 \ db_1 \ jdbc \ lib directory. The drivers for different databases are also different, you can search for the corresponding jar package public JDBCTest () {// TODO Auto-generated constructor stubtest ();} public st on the Internet. Atic void main (String [] args) {JDBCTest jdbc = new JDBCTest ();} public void test () {try {// 1. first load the driver // first find the Class where OrcalDriver is located. forName ("oracle. jdbc. driver. oracleDriver ");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // 2. configure the basic attribute String name = "scott"; // username String pass = "Ndbdta"; // password // connection statement, different databases have different connection statements: String url = "jdbc: oracle: thin: @ 127.0.0.1: 15 21: orcl "; // Connection type and host name Connection c = null; try {// use the driver to create a Connection c = DriverManager. getConnection (url, name, pass);} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // after the program has been connected to the database, // using PreparedStatement is much faster than statement. // PreparedStatement = null does not need to be precompiled; string SQL = ""; ResultSet rs = null; // you can directly modify try {SQL = "update emp set ename =? Where empno =? "; Statement = c. prepareStatement (SQL); // set? Replaced by the number of columns and type input value statement. setString (1, "jack"); statement. setInt (2, 7934); statement.exe cute ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // find try {SQL = "select * from emp "; statement = c. prepareStatement (SQL); rs = statement.exe cuteQuery ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} try {// before the result set first points to the first row, move to the first row while (rs. next () {int eno = rs. getInt (1); String ename = rs. getString (2); System. out. println (eno + "\ t" + ename) ;}} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.