Java database connection Daquan [turn]

xiaoxiao2021-03-06  60

I. JSP connection Oracle8 / 8i / 9i database (with Thin mode) TestoCle.jsp as follows: <% @ page contenttype = "text / html; charset = GB2312"%> <% @ Page Import = "java.sql. *" %> <% class.Forname ("Oracle.jdbc.driver.Oracledriver). NewInstance (); string url =" JDBC: Oracle: Thin: @localhost: 1521: ORCL "; // orcl SID String user to your database = "scott"; String password = "tiger"; Connection conn = DriverManager.getConnection (url, user, password); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String SQL = "Select * from test"; ResultSet RS = Stmt.executeQuery (SQL); while (rs.next ()) {%> Your first field content is: <% = rs.getstring (1)% > Your second field content is: <% = rs.getstring (2)%> <%}%> <% out.print ("Database operation success, congratulations");%> <% rs.close ); stmt.close (); conn.close ();%> 2, JSP connection SQL Server7.0 / 2000 database TestsqlServer.jsp is as follows: <% @ page contenttype = "text / html CHARSET = GB2312 "%> <% @ page import =" java.sql. * "%> <% class.forname (" com.microsoft.jdbc.sqlser.sqlserverdriver). NewInStance () String url = "JDBC: Microsoft: SQLserve R: // localhost: 1433; databasename = PUBS "; // pubs for your database String User =" SA "; string password ="; connection conn = drivermanager.getConnection (URL, User, Password); Statement Stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select * from test"; ResultSet rs = stmt.executeQuery (sql); while (rs.next ()) {%> your first Field content is: <

% = rs.getstring (1)%> Your second field content is: <% = rs.getstring (2)%> <%}%> <% out.print ("Database operation is successful, congratulations") ;%> <% rs.close (); stmt.close (); conn.close ();%> 3, JSP connection DB2 database TESTDB2.JSP is as follows: <% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.sql. *"%> <% class.forname ("com.ibm.db2.jdbc.app.db2driver ") .newinstance (); string url =" jdbc: db2: // localhost: 5000 / sample "; // Sample for your database name string user =" admin "; string password ="; connection conn = drivermanager. getConnection (url, user, password); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select * from test"; ResultSet rs = stmt.executeQuery (sql); while (rs.next () {%> Your first field content is: <% = rs.getstring (1)%> Your second field content is: <% = rs.getstring (2)%> <%}% > <% out.print ("Database Operation, Congratulations");%> <% rs.close (); stmt.close (); Conn.Close ();%> four , JSP connection Informix database Testinformix.jsp is as follows: <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ page import = "java.sql. *"%> <% Class.f orName ( "com.informix.jdbc.IfxDriver") newInstance (); String url =. "jdbc: informix-sqli: //123.45.67.89: 1533 / testDB: INFORMIXSERVER = myserver; user = testuser; password = testpassword"; // TestDb for your database name Connection Conn = DriverManager.getConnection (URL); Statement Stmt = Conn.createStatement (ResultSet.Type_Scroll_Sensitive, ResultSet.concur_updatable);

String SQL = "Select * from test"; ResultSet RS = Stmt.executeQuery (SQL); while (rs.next ()) {%> Your first field content is: <% = rs.getstring (1)% > Your second field content is: <% = rs.getstring (2)%> <%}%> <% out.print ("Database operation success, congratulations");%> <% rs.close ); stmt.close (); conn.close ();%>

============================================================================================================================================================================================================= ============================================================================================================================================================================================================= ======================================== Database connection Complete collection This is a basic article, mainly introduced JDBC Pop Database (SQL Server, Oracle) application. But it's all personal opinions, so I can't guarantee that it is completely correct, for reference only, if there is something different opinion, I can point out in the comments, I will make corresponding modifications. :) Under normal circumstances, when using the JDBC and other things, the code is compared to the commonly used database code may be: private connection conn private string poolur = ""; private string drivername = ""; private string user = "Private String Pass = ""; Private DBConnProxy Proxy; // Written Class, Control Database Access Scheme PUBLIC Object getConn () {Try {Properties Prop = New Properties (); Prop.SetProperty; Prop.SetProperty; Prop.SetProperty ("Password", pass); driver mydriver = (driver) Class.Forname (DRIVERNAME) .newinstance (); conn = mydriver.connect (poolurl, prop); // or conn = mydriver.connect (PoolUrl, NULL); When is the special manager with me? / * Or: class.Forname (drivername); conn = drivermanager.getConnection (PoolURL, User, Pass); * /} catch (Exception E) {system.out.println ("Open Database Error / N" E.getMessage ());} If (! proxy.getdb ()) {this.close () Return Null;} Return Conn;} DRIVERNAME, POOLURL, USER, PASS can be obtained by reading an XML file or a normal attribute file. But even SQLServer is still Oracle, just change the configuration.

They can be used in Sun.jdbc.odbc.jdbCodbcdriver. Corresponding to Pool = "JDBC: ODBC: Test", user, pass slightly can also be unique: sqlserver: weblogic.jdbc.msqlserver4.driver (online) corresponding poolurl = "JDBC: WebLogic: MSSQLServer4: CEIC@127.0.0.1 "// CEIC is the database name, 127.0.0.1 is a data source location, which can be a remote database, user, pass slightly oracle: Oracle.jdbc.driver.OracleDriver (Class12.zip, installed Oracle) corresponds to JDBC : Oracle: Thin: Scott/tiger@10.1.103.234: 1521: SP "Scott / Tiger connection identity username / password, 10.1.103.234, data source IP, 1521: Port (default is 1521). SP points to the database name User If the Pass is slightly used by the WebLogic database connection, it can pass CONTEXT CTX = New InitialContext (); DataSource DS = (DataSource) CTX.lookup ("ForceCDataSource"); // forceicDataSource is a matching DataSource. You can also pass and above Unified method to call. WebLogic: WebLogic.jdbc.pool.driver // drivername The corresponding poolURL = "JDBC: WebLogic: pool: poolname" // poolname is the name of the JDBC type in WebLogic .. IrsUser, Pass is the username and password of the WebLogic user (the user in the Administrators user group). (If not used with DriverManager, "conn = mydriver.connect (poolurl, null);" is in this case. I In WebLogic 6.1 test, it can be seen, it can be seen that most database connections can be flexible by configuring properties files. (Transplant is very convenient, and there is no impact on the application, of course, can also be boast Platform) So I can solve it by dynamically read attribute files. Private string javahome = system.getproperty ("JA Va.home "); private final string fs = system.getProperty (" file.separator "); private string path = javahome fs " db.properties "; // put db.properties under system properties Java.home, Generally is in the JRE directory under the JDK directory.

public boolean getInfo () {File file = new java.io.File (path); Properties pro = new Properties (); boolean readsucced; try {InputStream in = new BufferedInputStream (new FileInputStream (file)); pro.load (in ); drivername = propo.getProperty; poolur = pro.getProperty ("poolur") user = pro.getProperty ("user") pass = pro.GetProperty ("pass") // ourselves Trim Drop. I'm lazy. ReadSucced = true;} catch (Exception E) {system.out.println ("Read Properties File Error"); readsucced = false;} Return ReadSucced;}, but usually only read once. So you can put it in the Static {} block, or you can put it in the constructor (and then put it in a static connection pool, if it is a separate application, you should separate GetInfo () as a static class and database connection class). Public dbconn () {setdbconnproxy (proxyFactory.defaultType); // Setting up the connection proxy, control the database connection frequency, may sacrifice the effectiveness, but it is a bit role in preventing malicious destruction, and I can't write it on the WEBLOGIC connection. So I have to do some small precautions. If it is a connection pool with WebLogic, you can add anything that doesn't do anything directly in ProxyFactory. GetInfo ();} Property file db.properties content: drivername = sun.jdbc .odbc.jdbcodbcdriver pool = jdbc: ODBC: Test user = sa ourselves can campaign actual situation to change the properties file. Note: The getConn () method returns Object, which is a different extension application. For example, Oracle has the application for Clob, Blob (in the case of class12.zip), ordinary connection cannot be met, so It can be used by converting the Object returned in the application to OracleConnection. (Personal point of view, don't know if there is a better solution) so much, it may only be a bit used for novices, it is already very outdated in J2EE popularity, I also hope that the master will point out the problem (there is no about mysql The connection method has not been done, I hope someone can add it). More resources: Participate in the Forum Discussion: http://www.java-cn.com/index.jsp More Technical Articles: http://www.java-cn.com/technology/index.jspjava Chinese Station: We have to do China's best Java portal, remember our domain name: java-cn.com | java-cn.Net | java-cn.org this website URL: http://www.java-cn.com/technology /technology_detail.jsp?id=2891 Sound: Reprinted this article, you must indicate the original website of Java Chinese Station in a prominent location; if it is the reprinted article of this site, please indicate the original text, and retain the author information

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

New Post(0)