附程序主要部分供参考。这里仅写出一个init()方法,具体的界面请用AWT实现。
import java.net.*;
import java.sql.*;
public class dbexamp extends java.applet.Applet
{
public void 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(";Driver "+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.close();
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();
}
}
}