JSP pages in the database connectivity features (zz)

  Jsp many novice users often ask how database connectivity ah, I summarize for your reference, 
  In fact, all of this logic on the database jsp, is not necessarily good practice, but beginners to learn, when we learn a certain degree of, 
  MVC may consider using the model developed.    In practice these codes, you must be jdbc driver put in the path of the server, 
  And then to build a database table test, for example, there are two fields test1, test2, can be built using the following SQL 
  Create table test (test1 varchar (20), test2 varchar (20) 
  To the table and then write a test records, then we will now begin the journey jsp and databases it. 

  First, jsp Oracle8/8i/9i database connectivity (using thin model) 
  Testoracle.jsp as follows: 
  <% @ Page contentType = "text / html; gb2312 charset ="%> 
  <% @ Page import = "java.sql .*"%> 
<html>
<body>
  <% Class.forName ( "oracle.jdbc.driver.OracleDriver"). NewInstance (); 
  String url = "jdbc: oracle: thin: @ localhost: 1521: orcl"; 
  / / Orcl for your database SID 
  String user = "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 column says: <% = rs.getString (1)%> 
  Your second column says: <% = rs.getString (2)%> 
<%}%>
  <% Out.print ( "database operations successful, congratulate you ");%> 
  <% Rs.close (); 
  Stmt.close (); 
  Conn.close (); 
%>
  </ Body> 
  </ Html> 

  Second, jsp database connectivity Sql Server7.0/2000 
  Testsqlserver.jsp as follows: 
  <% @ Page contentType = "text / html; gb2312 charset ="%> 
  <% @ Page import = "java.sql .*"%> 
<html>
<body>
  <% Class.forName ( "com.microsoft.jdbc.sqlserver.SQLServerDriver"). NewInstance (); 
  String url = "jdbc: microsoft: sqlserver: / / 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 column says: <% = rs.getString (1)%> 
  Your second column says: <% = rs.getString (2)%> 
<%}%>
  <% Out.print ( "database operations successful, congratulate you ");%> 
  <% Rs.close (); 
  Stmt.close (); 
  Conn.close (); 

%>
  </ Body> 
  </ Html> 

  Third, jsp connect DB2 database 
  Testdb2.jsp as follows: 
  <% @ Page contentType = "text / html; gb2312 charset ="%> 
  <% @ Page import = "java.sql .*"%> 
<html>
<body>
  <% 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 column says: <% = rs.getString (1)%> 
  Your second column says: <% = rs.getString (2)%> 
<%}%>
  <% Out.print ( "database operations successful, congratulate you ");%> 
  <% Rs.close (); 
  Stmt.close (); 
  Conn.close (); 
%>
  </ Body> 
  </ Html> 
  4. Jsp Informix database connection 
  Testinformix.jsp as follows: 
  <% @ Page contentType = "text / html; gb2312 charset ="%> 
  <% @ Page import = "java.sql .*"%> 
<html>
<body>
  <% Class.forName ( "com.informix.jdbc.IfxDriver"). NewInstance (); 
  String url = 
  "Jdbc: informix-sqli: / / 123.45.67.89:1533 / testDB: INFORMIXSERVER = myserver; 
  User = testuser; testpassword password = "; 
  / / 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 column says: <% = rs.getString (1)%> 
  Your second column says: <% = rs.getString (2)%> 
<%}%>
  <% Out.print ( "database operations successful, congratulate you ");%> 
  <% Rs.close (); 
  Stmt.close (); 
  Conn.close (); 
%>
  </ Body> 
  </ Html> 

  5. Jsp Sybase database connection 
  Testmysql.jsp as follows: 
  <% @ Page contentType = "text / html; gb2312 charset ="%> 
  <% @ Page import = "java.sql .*"%> 
<html>
<body>
  <% Class.forName ( "com.sybase.jdbc.SybDriver"). NewInstance (); 
  String url = "jdbc: sybase: Tds: localhost: 5007/tsdata"; 
  / / Tsdata for your database name 
  Properties sysProps = System.getProperties (); 
  SysProps.put ( "user", "userid"); 
  SysProps.put ( "password", "user_password"); 
  Connection conn = DriverManager.getConnection (url, SysProps); 
  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 column says: <% = rs.getString (1)%> 
  Your second column says: <% = rs.getString (2)%> 
<%}%>
  <% Out.print ( "database operations successful, congratulate you ");%> 
  <% Rs.close (); 
  Stmt.close (); 
  Conn.close (); 
%>
  </ Body> 
  </ Html> 

  6. Jsp MySQL database connection 
  Testmysql.jsp as follows: 
  <% @ Page contentType = "text / html; gb2312 charset ="%> 
  <% @ Page import = "java.sql .*"%> 
<html>
<body>
  <% Class.forName ( "org.gjt.mm.mysql.Driver"). NewInstance (); 
  String url = "jdbc: mysql: / / localhost / softforum? User & password = = soft soft1234 & useUnicode = = true & characterEncoding 8859_1" 
  / / 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 column says: <% = rs.getString (1)%> 
  Your second column says: <% = rs.getString (2)%> 
<%}%>
  <% Out.print ( "database operations successful, congratulate you ");%> 
  <% Rs.close (); 
  Stmt.close (); 
  Conn.close (); 
%>
  </ Body> 
  </ Html> 

  7. Jsp PostgreSQL database connection 
  Testmysql.jsp as follows: 
  <% @ Page contentType = "text / html; gb2312 charset ="%> 
  <% @ Page import = "java.sql .*"%> 
<html>
<body>
  <% Class.forName ( "org.postgresql.Driver"). NewInstance (); 
  String url = "jdbc: postgresql: / / localhost / soft" 
  / / Soft for your database name 
  String user = "myuser"; 
  String password = "mypassword"; 
  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 column says: <% = rs.getString (1)%> 
  Your second column says: <% = rs.getString (2)%> 
<%}%>
  <% Out.print ( "database operations successful, congratulate you ");%> 
  <% Rs.close (); 
  Stmt.close (); 
  Conn.close (); 
%>
  </ Body> 
  </ Html> 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Recommend Articles

Comments

Leave a Reply