NetBeans做数据库操作
来介绍一下NetBeans进行数据库操作,其实基本原理跟eclipse一样的,但是两者还是有稍微的区别的。下面请看我慢慢讲解吧!
开发环境:NetBeans IDE 6.0.1 + SQL Server 2005 + JDK 5.0以上
打开 NetBeans IDE 6.0.1,首先要导入sqljdbc包
![]()
这样可以将sqljdbc.jar导入到我们的项目中来,以便之后的开发使用!
添加完后,显示如下
![]()
向 "源包" 中添加文件,结果如下
![]()
Config.properties 属性配置文件
#数据库驱动
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#给定数据库 URL 的连接
url=jdbc:sqlserver://localhost;DatabaseName=WorldCup2006
#数据库登录用户名
user=ayumi
#登录数据库密码
password=ayumi
DataAccess.java 数据库访问类
/**
* DataAccess.java 数据库访问类
*
* Created on 2007年10月14日, 上午10:11
* Author ajayumi
* Description : 该类主要是用于做数据库操作,可以直接使用该类便可完成绝大部分的数据库访问操作
*/
package DemoApp1;
![]()
import java.sql.*;
import java.io.*;
import java.util.Properties;
![]()
/**
*
* @author ajayumi
*
*/
public class DataAccess
{
// 私有变量*****************************************************
/** 驱动连接串 */
private String driver = null;
/** jdbc:subprotocol:subname 形式的数据库 url */
private String url = null;
/** 数据库用户,连接是为该用户建立的 */
private String user = null;
/** 用户的密码 */
private String password = null;
/** 属性文件存放路径 */
private String PropertiesFilePath = null;
/** 数据库连接对象 */
private Connection Conn = null;
// *****************************************************
public DataAccess()
{
}
![]()
/**
* 创建一个带参数的构造函数
*
* @param _driver,驱动连接串
* @param _url,jdbc:subprotocol:subname形式的数据库
* url
* @param _user,数据库用户,连接是为该用户建立的
* @param _password,用户的密码
*/
public DataAccess(String _driver, String _url, String _user,
String _password)
{
this.SetDriver(_driver);
this.SetUrl(_url);
this.SetUser(_user);
this.SetPassword(_password);
}
![]()
/**
* 创建一个带参数的构造函数
*
* @param _PropertiesFilePath,属性文件存放路径
*/
public DataAccess(String _PropertiesFilePath)
{
this.SetPropertiesFilePath(_PropertiesFilePath);
}
![]()
// 属性设置*****************************************************
/**
* 获取该类中的 driver
*
* @return driver
*/
public String getDriver()
{
return this.driver;
}
![]()
/**
* 设置类中的 driver 为 _driver
*
* @param _driver,新建_driver
*/
public void SetDriver(String _driver)
{
if (_driver.equals("") && _driver == null)
{
System.out.println("No Setting Driver!");
} else
{
this.driver = _driver;
}
}
![]()
/**
* 获取该类中的数据库 URL 的连接
*
* @return url
*/
public String getUrl()
{
return this.url;
}
![]()
/**
* 设置类中的 url 为 _url
*
* @param _url,新建_url
*/
public void SetUrl(String _url)
{
if (_url.equals("") && _url == null)
{
System.out.println("No Setting Url!");
} else
{
this.url = _url;
}
}
![]()
/**
* 获取该类中的数据库用户,连接是为该用户建立的
*
* @return user
*/
public String getUser()
{
return this.user;
}
![]()
/**
* 设置类中的 user 为 _user
*
* @param _user,新建_user
*/
public void SetUser(String _user)
{
if (_user.equals("") && _user == null)
{
System.out.println("No Setting User!");
} else
{
this.user = _user;
}
}
![]()
/**
* 获取该类中的数据库用户的密码
*
* @return password
*/
public String getPassword()
{
return this.password;
}
![]()
/**
* 设置类中的 password 为 _password
*
* @param _password,新建_password
*/
public void SetPassword(String _password)
{
if (_password.equals("") && _password == null)
{
System.out.println("No Setting Password!");
} else
{
this.password = _password;
}
}
![]()
/**
* 获取该类中的属性文件存放路径
*
* @return PropertiesFilePath
*/
public String getPropertiesFilePath()
{
return this.PropertiesFilePath;
}
![]()
/**
* 设置类中的 PropertiesFilePath 为 _PropertiesFilePath
*
* @param _PropertiesFilePath,新建_PropertiesFilePath
*/
public void SetPropertiesFilePath(String _PropertiesFilePath)
{
if (_PropertiesFilePath.equals("") && _PropertiesFilePath == null)
{
System.out.println("No Setting PropertiesFilePath!");
} else
{
this.PropertiesFilePath = _PropertiesFilePath;
}
}
![]()
/**
* 获取属性文件的数据库连接信息
*/
public void GetProperty() throws FileNotFoundException, IOException,
Exception
{
Properties pro = new Properties();
try
{
FileInputStream in = new FileInputStream(this.PropertiesFilePath);
pro.load(in);
this.driver = pro.getProperty("driver");
this.url = pro.getProperty("url");
this.user = pro.getProperty("user");
this.password = pro.getProperty("password");
} catch (FileNotFoundException e)
{
throw new FileNotFoundException("FileNotFoundException Error Message :\n" + e.getMessage());
} catch (IOException e)
{
throw new IOException("IOException Error Message :\n" + e.getMessage());
} catch (Exception e)
{
throw new Exception("Other Error Message :\n" + e.getMessage());
}
}
![]()
/**
* 返回数据库连接
*/
public Connection GetConnection() throws Exception
{
try
{
Class.forName(this.driver); // 显式地加载 JDBC 驱动程序
this.Conn = DriverManager.getConnection(this.url, this.user,
this.password);
} catch (Exception e)
{
throw new Exception("DataBase Connect Error Message :\n" + e.getMessage());
}
return this.Conn;
}
![]()
/**
* 返回一个数据集
*
* @param _Conn,数据库连接对象
* @param _SQLstr,SQL语句
*/
public ResultSet GetRs(Connection _Conn, String _SQLstr)
throws SQLException, Exception
{
Statement st = null;
ResultSet rs = null;
try
{
st = _Conn.createStatement();
rs = st.executeQuery(_SQLstr);
} catch (SQLException e)
{
throw new SQLException("DataBase Error Message :\n" + e.getMessage());
} catch (Exception e)
{
throw new Exception("Other Error Message :\n" + e.getMessage());
} finally
{
}
return rs;
}
![]()
/** 输出方法 */
public void PrintProperties()
{
System.out.println("Driver:" + this.driver);
System.out.println("Url:" + this.url);
System.out.println("User:" + this.user);
System.out.println("Password:" + this.password);
}
}
Main.java 测试程序的主类
/*
* 测试文件主类
*/
package DemoApp1;
![]()
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
![]()
/**
*
* @author ajayumi
*/
public class Main extends JFrame
{
![]()
private JPanel panel = null;
private JLabel lb1 = null;
private JLabel lb2 = null;
private JLabel lb3 = null;
private JTextField txtCol1 = null;
private JTextField txtCol2 = null;
private JTextField txtSQLstr = null;
private JTextArea txt = null;
private JButton btn = null;
![]()
public Main()
{
super("简单数据库访问程序");
![]()
this.setLayout(new java.awt.BorderLayout());
this.setVisible(true);
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100, 100, 600, 300);
![]()
this.panel = new JPanel();
this.panel.setLayout(new java.awt.FlowLayout());
this.lb1 = new JLabel("SQL语句:");
this.lb2 = new JLabel("列名1:");
this.lb3 = new JLabel("列名2:");
this.txtSQLstr = new JTextField("Select * from team", 15);
this.txtCol1 = new JTextField("group1", 5);
this.txtCol2 = new JTextField("team1", 5);
this.panel.add(this.lb1);
this.panel.add(this.txtSQLstr);
this.panel.add(this.lb2);
this.panel.add(this.txtCol1);
this.panel.add(this.lb3);
this.panel.add(this.txtCol2);
![]()
this.txt = new JTextArea(5, 10);
this.btn = new JButton("显示数据");
this.add(this.panel, java.awt.BorderLayout.NORTH);
this.add(this.txt, java.awt.BorderLayout.CENTER);
this.add(this.btn, java.awt.BorderLayout.SOUTH);
![]()
this.btn.addActionListener(new ActionListener()
{
![]()
public void actionPerformed(ActionEvent e)
{
btn_Clicked(e);
}
});
this.pack();
}
![]()
private void btn_Clicked(ActionEvent e)
{
try
{
DataAccess da = new DataAccess("Config.properties");//调用带一个参数的构造函数,创建 da 对象
da.GetProperty();
Connection conn = da.GetConnection();
![]()
ResultSet rs = da.GetRs(conn, this.txtSQLstr.getText());
while (rs.next())
{
String group1 = rs.getString(this.txtCol1.getText());
String team1 = rs.getString(this.txtCol2.getText());
![]()
this.txt.append(String.format("%s\t%s\n", group1, team1));
}
![]()
rs.close();
} catch (Exception ex)
{
this.txt.setText(ex.getMessage());
}
}
![]()
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
new Main();
}
}
程序运行后,界面如下:
![]()
说明一下,要使用本程序,首先要配置目录下的的Config.properties文件,使用记事本打开,可以你会看到一下类似乱码的东西,但是不要紧,那些不是乱码,只是一些编码问题,造成那种情况的。我们只要修改几处关键地方就行啦!将DataBaseName=xxxxx改了,User=xxxxx改了,Password=xxxxx改了,保存就可以了!
然后远行程序的时候,自己编写Select语句,对指定的表进行数据查询,后面的两个文本框是数据表中的字段名,填写完就可以单击按钮啦!
示例下载
开发环境:NetBeans IDE 6.0.1 + SQL Server 2005 + JDK 5.0以上
打开 NetBeans IDE 6.0.1,首先要导入sqljdbc包
这样可以将sqljdbc.jar导入到我们的项目中来,以便之后的开发使用!
添加完后,显示如下

向 "源包" 中添加文件,结果如下

Config.properties 属性配置文件









DataAccess.java 数据库访问类






















































































































































































































































































Main.java 测试程序的主类




































































































程序运行后,界面如下:

说明一下,要使用本程序,首先要配置目录下的的Config.properties文件,使用记事本打开,可以你会看到一下类似乱码的东西,但是不要紧,那些不是乱码,只是一些编码问题,造成那种情况的。我们只要修改几处关键地方就行啦!将DataBaseName=xxxxx改了,User=xxxxx改了,Password=xxxxx改了,保存就可以了!
然后远行程序的时候,自己编写Select语句,对指定的表进行数据查询,后面的两个文本框是数据表中的字段名,填写完就可以单击按钮啦!
示例下载