General steps to access database programs

xiaoxiao2021-03-06  64

// 1 Write the steps to access the database program:

// 1-1 The JDBC driver of the load database // where JDBC-ODBC Driver is from the JDK, and it has been registered by default // loaded jdbcodbdriverclass.Forname ("Sun.jdbc.odbc.jdbCodbDriver");

// Load and register SQLServer Driverclass.Forname ("com.microsoft.jdbc.sqlser.sqlserverdriver); java.sql.driverManager.registerDriver (new com.microsoft.jdbc.sqlserver.sqlserverDriver ());

// Load and register ORACLEDRIVERCLASS.Forname ("Oracle.jdbc.Driver.Oracledriver); java.sql.driverManager.RegisterDriver (New Oracle.jdbc.driver.Orcledriver ());

// Load and register mysqldriverclass.forname ("com.mysql.jdbc.driver); // is not a necessary step, because some drivers are registered while loading, so you don't have to register //, such as mysqldriverjava. Sql.driverManager.registerDriver (new com.mysql.jdbc.driver ());

// 1-2 Connection Connection Con = Java.sql.driverManager.getConnection (JDBC URL, User Name, Password)

// JDBC URL: // The JDBC URL connection form for several common databases is as follows: JDBC: ODBC: Datasource

JDBC: Oracle: Thin: @localhost: 1521: SID

JDBC: Microsoft; SQLServer: // localhost: 1433; DatabaseName = BOOKDB

JDBC: mysql: // localhost: 3306 / bookdb

// 1-3 Create a statement or prepareStatement object statement stmt = con.createstatement ();

PrepareStatement prepstmt = con.preparestatement (SelectStatement);

// 1-4 call SQL statement string SQL = "SELECT ID, NAME, TITLE, Price from Books where = 'TOM' AND price = 40"; ResultSet RS = Stmt.executeQuery (SQL);

// 1-5 Access ResultSetWhile (Rs.Next ()) {string col1 = rs.getstring (1); string col2 = rs.getstring (2); string col3 = rs.getstring (3); float col4 = rs. Getfloat (4);

// 1-6 Close the ResultSet, Statement, Connection object Rs.close (); CON.CLOSE (); CON.CLOSE ();

转载请注明原文地址:https://www.9cbs.com/read-110067.html

New Post(0)