Using JDBC to implement SQL Server via applets

Source: Internet
Author: User
Tags odbc mssql stmt

JDBC is already included in a later version of JDK1.1 and can be used for access to the database.

for accessing MSSQL. After an ODBC data source is established, JDBC is used to implement access to the database.

The main related classes are in the java.sql package.

Let's say we now have a library named company on MSSQL, and one of the tables is tb_name. We would like to remove the corresponding record name from this table by number numbers. A corresponding ODBC data source has been established for Javadata.

You want to complete the operation with an applet.

The main part of the procedure is for reference. Only one init () method is written here, and the concrete interface is implemented with AWT.

Import java.net.*
Import java.sql.*
public class Dbexamp extends Java.applet.Applet
{
Public v OID init () {
String url= "Jdbc:odbc:javadata";
try{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=drivermanager.getconnection (URL, "sa", "");//mssql database user sa and
Password
DatabaseMetaData Dma=con.getmetadata ();
System.out.println ("Connect to" +dma.geturl ());
System.out.println (";D River" +dma.getdrivername ());
System.out.println ("; Version "+dma.getdriverversion ());
System.out.println ("");
Statement stmt=con.createstatement ();
ResultSet rs=stmt.executequery ("select * from Company.dbo.TB_NAME where number=1");//sql
Rs.next ();
String dispresult=rs.getstring ("name");
System.out.println (Dispresult);//instead,you can display it in Paint () or use AWT etc.
Rs.close ();
Stmt.clos E ();
Con.close ();
}
catch (SQLException ex) {
SYSTEM.OUT.PRINTLN ("!!! SQL Exception!!! ");
while (Ex!=null) {
System.out.println ("SQLState:" +ex.getsqlstate ());
SYSTEM.OUT.PRINTLN ("Message:" +ex.getmessage ());
System.out.println ("Vendor:" +ex.geterrorcode ());
Ex=ex.getnextexception ();
System.out.println ("");
}
}
catch (Java.lang.Exception ex) {
Ex.printstacktrace ();
}
}
}

Related Article

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.