日期:2014-05-20  浏览次数:20638 次

帮帮忙吧!!!!
public static int modifyRecord(String strProcInvoke) {
Connection con = ConnectionManager.getConnection();
try {
return con.prepareCall("{call " + strProcInvoke + "}").executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return 0;
} finally {
ConnectionManager.closeConnection(con);
}
}


如何调用上述方法实现按钮添加事件
------解决方案--------------------
你的按钮是Button还是JButton,点击按钮连接数据库操作吗;
1、 final Button button = new  Button(parent, style);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
                   
        调用函数:modifyRecord(String strProcInvoke)
                      }
});

2、final  JButton button = new JButton();
  
  button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
                      调用函数:modifyRecord(String strProcInvoke)
                     }
              });

------解决方案--------------------
引用:
public static int modifyRecord(String strProcInvoke) {
Connection con = ConnectionManager.getConnection();
try {
return con.prepareCall("{call " + strProcInvoke + "}").executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return 0;
} finally {
ConnectionManager.closeConnection(con);
}   //以上代码单独在一个类内
}





// 添加按钮
private JButton btnAdd = null;
btnAdd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
 
if (e.getActionCommand().equals("btnAdd")) {

这里面该如何写呢????????????????怎么才能往数据库内添加记录啊
}


“这里面该如何写呢??????”:这个位置就写你的那个   类名.modifyRecord(String strProcInvoke)//因为modifyRecord(String strProcInvoke)是static的


给你借鉴一下我以前操作数据库的方法,注意sql地方就是你可以对数据库进行的操作
public class Dao {
protected static String dbClassName = "com.mysql.jdbc.Driver";
protected static String dbUrl = "jdbc:mysql://localhost:3306/db_library";
protected static String dbUser = "root";
protected static String dbPwd = "rot";
protected static String second = null;
private static Connection conn = null;

public Dao() {
try {
if (conn == null) {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);

} else
return;
} catch (Exception ee) {
ee.printStackTrace();
}

}

private static ResultSet executeQuery(String sql) {
try {
if (conn == null)
new Dao();
return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
}
}

private static int executeUpdate(String sql) {

try {
if (conn == null)
new Dao();
return conn.createStatement().executeUpdate(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());

return -1;
} finally {
}
}
//下面是一个如何用sql语句对数据库操作的示列
public static List check(String name, String sex, int age) {
// int i = 0;
List list = new ArrayList();

String sql = "select *  from tb_operator where name='" + name
+ "' and sex='" + sex + "'and age=" + age;

ResultSet rs = Dao.executeQuery(sql);//这里就是使用sql语句对数据库进行的操作
try {
while (rs.next()) {
People person = new People();

person.setId(rs.getString("id"));
person.setName(rs.getString("name"));
person.setSex(rs.getString("sex"));