Use JavaBean for Database Connectivity (reprint) __c#

Source: Internet
Author: User
Tags stmt

The database used in this example is SQL Server book


Java source file:

package jdbc;
Import java.sql.*;
public class Jdbcbean
{
 string dbdriver= "Com.microsoft.jdbc.sqlserver.SQLServerDriver";
 string url= "JDBC:MICROSOFT:SQLSERVER://LOCALHOST:1433;DATABASENAME=DWJJ";
 string username= "sa";
 string password= "";  
 
 connection con=null; 
 statement stmt=null;
 resultset Rs=null;


Instantiating, loading the JDBC driver
Public Jdbcbean ()
{
Try
{
Class.forName (Dbdriver);
}
catch (ClassNotFoundException e) {}
}


Returns the data set after the query
Public ResultSet executequery (String sql)
{
Try
{
Con=drivermanager.getconnection (Url,username,password);
Stmt=con.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);
Rs=stmt.executequery (SQL);
}
catch (SQLException e) {}

Return RS;
}


Close Database
public void Closedb ()
{
try{
Rs.close ();
Con.close ();
}
catch (SQLException e) {}
}
}

Test files:

<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<title>test bean</title>
<body>
<jsp:usebean id= "jdbc" class= "jdbc". Jdbcbean "scope=" page/>

<%


String sql= "SELECT * from Member_base ORDER by mb_regtime Desc";
ResultSet rs=jdbc.executequery (SQL);

while (Rs.next ())
{
Out.println (rs.getstring ("mb_pwd") + "<br/>");
}

%>

</body>

///////

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.