1个回答
展开全部
//此类为连接数据库并进行数据库的操作
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Conn {
private static Connection conn = null;
private static Statement st = null;
private static ResultSet rs = null;
//建立数据库的连接
public Conn(){
String url = "jdbc:sqlserver://localhost:1433;databaseName=ZYGX";
String user = "sa";
String password = "123";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, user, password);
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 通过不同 的sql语句,得到相应Resultset结果集
public ResultSet getRs(String sql){
try{
rs= st.executeQuery(sql);
}catch(SQLException e){
e.printStackTrace();
}
return rs;
}
// 根据不同的sql语句,执行数据库的更新操作
public int updata(String sql){
int num=0;
try{
num = st.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
return num;
}
// 关闭数据库连接相应的资源
public void close(){
try{
if(rs!=null){
rs.close();
rs = null;
}
if(st!=null){
st.close();
st = null;
}
if(conn!=null){
conn.close();
conn = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
----------------------------------------------------------------------
//可以对button里添加动作按钮:
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
Conn conn =new Conn();
String sql1="select * from aa where name='"+name+"' ";//按name值查找
ResultSet rs = conn.getRs(sql1);
try {
while(rs.next()){
int n=rs.getString("type");
}
} catch (SQLException e) {
e.printStackTrace();
}
String name=textField.getText();
String sql="update aa set tittle='"+name+"' ";//从aa表将title字段的值改成textField里的name值
String sql2 ="delete from aa where name='"+name+"'";//从aa表将按取得name的值删除该行数据
String sql3 = "insert into aa (name,uname) values ('"+name+"','"')"; //将name,uname值新增到aa表
if(conn.update(sql)==1){
System.out.print("修改成功");
}
if(conn.update(sql2)==1){
System.out.print("删除成功");
}
if(conn.update(sql3)==1){
System.out.print("新增成功");
}
}
});
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Conn {
private static Connection conn = null;
private static Statement st = null;
private static ResultSet rs = null;
//建立数据库的连接
public Conn(){
String url = "jdbc:sqlserver://localhost:1433;databaseName=ZYGX";
String user = "sa";
String password = "123";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, user, password);
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 通过不同 的sql语句,得到相应Resultset结果集
public ResultSet getRs(String sql){
try{
rs= st.executeQuery(sql);
}catch(SQLException e){
e.printStackTrace();
}
return rs;
}
// 根据不同的sql语句,执行数据库的更新操作
public int updata(String sql){
int num=0;
try{
num = st.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
return num;
}
// 关闭数据库连接相应的资源
public void close(){
try{
if(rs!=null){
rs.close();
rs = null;
}
if(st!=null){
st.close();
st = null;
}
if(conn!=null){
conn.close();
conn = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
----------------------------------------------------------------------
//可以对button里添加动作按钮:
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
Conn conn =new Conn();
String sql1="select * from aa where name='"+name+"' ";//按name值查找
ResultSet rs = conn.getRs(sql1);
try {
while(rs.next()){
int n=rs.getString("type");
}
} catch (SQLException e) {
e.printStackTrace();
}
String name=textField.getText();
String sql="update aa set tittle='"+name+"' ";//从aa表将title字段的值改成textField里的name值
String sql2 ="delete from aa where name='"+name+"'";//从aa表将按取得name的值删除该行数据
String sql3 = "insert into aa (name,uname) values ('"+name+"','"')"; //将name,uname值新增到aa表
if(conn.update(sql)==1){
System.out.print("修改成功");
}
if(conn.update(sql2)==1){
System.out.print("删除成功");
}
if(conn.update(sql3)==1){
System.out.print("新增成功");
}
}
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询