Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
/** * Jdbc:java Database link basic process of using JDBC to connect to a database (use Oracle here) * 1: Load Drive class * 2: Connect to Database * 3: Execute SQL statement * 4: Process Result set * 5: Close connection * * @author ^_^ * */public class Jdbcdemo {public static void main (string[] args) {String orc= "oracle.jdbc.driver.
Oracledriver ";
Java.sal.Connection//Represents the connection to the database Connection conn=null;
try {* * * 1: * Load driver * Need to note that different database, parameter that string content is different *
* This step may be thrown after execution: * ClassNotFoundException exception * appears as follows: * 1: Database-driven jar packages are not imported into environment variables
* 2:class.forname string spelling error/Class.forName (ORC);
SYSTEM.OUT.PRINTLN ("Drive loaded successfully"); * * 2: Establish a connection with the database via DriverManager * Its static method getconnection for getting the connection * typically requires three parameters * Parameter 1: The address of the database and the port (different database string contents are not identical
) * Format: Jdbc:oracle:thin: @host:p Ort:sid* Parameter 2: Database user name * Parameter 3: Database Password/conn=drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:xe",//
The address of the database and its port "system",//Database username "8nianji7ban"//Database password);
SYSTEM.OUT.PRINTLN ("Database already connected");
* * 3:java.sql.statement * This class is used to execute the SQL statement and obtain the result * * Statement state =conn.createstatement ();
System.out.println ("QQQQQ");
String sql= "Select Ename,job,sal,deptno from emp";//Current DATABASE statement System.out.println (SQL) to be executed; * * Statement provides different methods of execution for different SQL statements: * ResultSet executequery (String aql) * This method is specifically used to execute the DQL statement, and the returned ResultSet represents the knot of the query Fruit set * * int executeupdate (String sql) * This method is specifically used to execute a DML statement, and the number returned indicates how many data in the table have been affected by the execution of the statement * * Boolean execute (St Ring SQL) * This method theoretically any statement can be run, but since Dql,dml has a special method, so this method is usually used to execute DDL statements * The return value of the method indicates whether there is a result set after executing the SQL statement, the existence returns TRUE * if the SQL statement itself
There is a mistake, throw the exception directly: SQLException * * SYSTEM.OUT.PRINTLN ("executing query");
ResultSet rs=state.executequery (SQL),//Returns a result set SYSTEM.OUT.PRINTLN ("Query completed"); * * 4: Traverse result set (checkQuery results) * Resulset provides methods for traversing result sets * Boolean next () * This method has two effects, first when we query the result set the RS pointer to the first data, so we need to first call next () *
To move the pointer over the first piece of data, and represents the data * The second job is to look at the return value, if the pointer moves down and finds no data, returns FALSE, and returns true if there is one, so we get the values for each field in the current record only when the method returns True * * RS also provides a number of getxxx (String fieldName) * Methods: * This method is used to get the value of the given field in the current record represented by RS, and to invoke the corresponding method for different fields due to different types *
For example: The VARCHAR2 type invokes getString/while (Rs.next ()) {//Get Employee name String ename=rs.getstring ("ename");
String job=rs.getstring ("job");
int Sal=rs.getint ("Sal");
int Deptno=rs.getint ("Deptno");
System.out.println (ename+ "," +job+ "," +sal+ "," +deptno ");
} rs.close ();
State.close ();
catch (Exception e) {e.printstacktrace ();
Todo:handle exception}finally{if (conn!=null) {try {conn.close ();
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
}
}
}
}
}
Note more detailed, others do not repeat.