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 ();
}
}
}