accessing databases using Java applets

Source: Internet
Author: User
Tags odbc access database microsoft access database

Learn any program language, of course, with the database, Java just born, the support of the database is not very good, after several years of development, its support for the database has been fully reached the ripe situation. Because this is mainly to introduce Java applets applet, therefore, it is not possible to use a large space to introduce the knowledge of the database, how to build and design a database, or please find some books to see their own! Here, I can't.

Here we use the Microsoft Access database as an example to illustrate how to implement a database open and read knowledge.

First step:

Use Microsoft Access to create a database, which is what I use for this example.

The second step: that is, the process of editing.

1 define variables, and the variables are defined as follows:

TextArea theVisits=new TextArea (6,80);
//显示数据库的内容
TextField theStatus=new TextField ("",80);
//显示打开数据库的信息。相当于程序的状态栏
Connection theConnection; //数据库的连接方法
Statement theStatement; //代表一个发送到数据库来执行的数据库命令
ResultSet theResult; //读取的数据结果,也就是数据库返回的结果
ResultSetMetaData theMetaData;
//包含了有关数据库命令执行后 返回结果的有用信息。
String theDataSource; //包含了被访问数据库或者数据源的名称,用URL形式表示 .
String theUser; //数据库的用户名
String thePassword; //数据库的密码

2 implementation of the interface, you can start to introduce the method to achieve, the interface of the implementation of relatively simple, the following:

public void init()
{
add(theVisits);
add(theStatus);
theVisits.setEditable (false); //设置文本区域不可以被用户写入
theStatus.setEditable (false); //设置文本区域不可以被用户写入
openConnection(); //打开数据库的连接
execSqlCommand("select * from MyTable"); //从数据库中读取内容
closeConnection(); //关闭已经打开的数据库
}

3 Open a connection to a database

public void openConnection()
{
theDataSource="jdbc:odbc:MyAccess";
theUser="";
thePassword="";
try
{
Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
theConnection=DriverManager.getConnection(theDataSource,theUser,thePassword);
theStatus.setText("Status:OK");
}
catch (Exception e)
{
handleException(e);
}
}

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.