java之修改并选择显示SQL Server数据库中的数据

package test;

import java.util.*;
import javax.swing.*;
import java.sql.*;

public class DataBaseDemo extends JFrame{

	private Vector rowData, columnName;
	private JTable jt = null;
	private JScrollPane jsp = null;
	
	//定义操作数据库所需要的对象
	private PreparedStatement ps = null;
	Connection ct = null;
	ResultSet rs = null;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		DataBaseDemo dbd = new DataBaseDemo();
	}
	
	public DataBaseDemo(){
		try{
			//加载驱动
			//com.microsoft.sqlserver.jdbc.SQLServerDriver
			//jdbc:sqlserver://127.0.0.1:1433;
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=AnimalManagementSys", "sa", "tu520");
			//ps = ct.prepareStatement("insert into AnimalInfo values('d0014', '金乌', '雌', 2600, '太阳', '太阳真火')");
			
			ps = ct.prepareStatement("update AnimalInfo " +
					"set aniTalent = '三味真火' where aniName = '金乌' ");
			ps.executeUpdate();
			ps = ct.prepareStatement("select * from AnimalInfo where aniSex = '雌' and aniAge between 800 and 3000 ");
			rs = ps.executeQuery();
			
			columnName = new Vector();
			//设置列名
			columnName.add("aniId");
			columnName.add("aniName");
			columnName.add("aniSex");
			columnName.add("aniAge");
			columnName.add("aniHome");
			columnName.add("aniTalent");
			
			rowData = new Vector();
			
			while(rs.next()){

				Vector line1 = new Vector();
				line1.add(rs.getString(1));
				line1.add(rs.getString(2));
				line1.add(rs.getString(3));
				line1.add(String.valueOf(rs.getInt(4)));
				line1.add(rs.getString(5));
				line1.add(rs.getString(6));
				
				rowData.add(line1);
			}
			
			//初始化JTable
			jt = new JTable(rowData, columnName);
			
			jsp = new JScrollPane(jt);
			
			this.add(jsp);

			this.setTitle("你好啊!");
			this.setSize(400, 250);
			this.setLocation(300, 200);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setResizable(false);
			this.setVisible(true);
			
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			//关闭数据库
			try{
				if(rs != null)	rs.close();
				if(ps != null)	ps.close();
				if(ct != null)	ct.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

}


阅读更多
版权声明:欢迎转载,附上原文链接即可! https://blog.csdn.net/qq_21478795/article/details/51685277
文章标签: java SQL 修改数据库
个人分类: 学习交流
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭