C#数据库和java文本处理

java文本处理

package scut2018;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.Map;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Comparator;// 特别注意


import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;

public class main4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//文件路径
		String inputPath = "C:\\Users\\Timi\\Desktop\\input.txt";
		String outputPath = "C:\\Users\\Timi\\Desktop\\output.xml";
		
		//TreeMap 字典
		Map<String, ArrayList<String>> students = new TreeMap<>(new Comparator<String>(){
			public int compare(String o1, String o2)
			{
				return o2.compareTo(o1);
			}
		});
		
		//读文件
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath)));
			String line = null;
			while((line= br.readLine()) != null)
			{
				String stuId = line.split(",")[0];
				//已存在该学号
				if(students.containsKey(stuId))
				{
					students.get(stuId).add(line);
				}
				else//不存在学号则新建
				{
					students.put(stuId, new ArrayList<String>());
					students.get(stuId).add(line);
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		//写文件
		Document document = new Document();
		Element stus = new Element("students");
		document.addContent(stus);
		String stuid = null, stuname = null, coursename = null;
		int score;
		
		String [] info ;
		for(String key: students.keySet()) {
			Element stu  = new Element("student");
			
			info = students.get(key).get(0).split(",");
			stuid = info[0];
			stuname = info[1];
			
			Element estuid = new Element("学号");
			estuid.setText(stuid);
			stu.addContent(estuid);
			
			Element estuname = new Element("姓名");
			estuname.setText(stuname);
			stu.addContent(estuname);
			
			for(String line: students.get(key))
			{
				info = line.split(",");
				coursename = info[2];
				score = Integer.valueOf(info[3]);
				
				Element ecourse = new Element("课程");
				ecourse.setAttribute("课程", coursename);
				ecourse.setText(String.valueOf(score));
				stu.addContent(ecourse);
			}
			stus.addContent(stu);
		}
		
		try {
			XMLOutputter writer = new XMLOutputter();
			
			writer.output(document, new FileOutputStream(outputPath));
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}


## **C# 数据库**

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace school1
{
    public partial class Form1 : Form
    {
        //创建数据库连接
        static String strConn = "server=.; database=school1; integrated security=SSPI";
        SqlConnection conn = new SqlConnection(strConn);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string name = box1.Text.ToString();
            string name = comboBox1.SelectedItem.ToString();
            string strSql = @"select student.stuName, course.*,courseTable.score
                                from student, course, courseTable
                               where student.stuName = '" + name + @"'
                                and student.stuId = courseTable.stuId
                                and courseTable.courseId = course.courseId";

            //打开连接
            if(conn.State == System.Data.ConnectionState.Closed)
                conn.Open();

            //读取数据
            SqlDataAdapter da = new SqlDataAdapter(strSql, conn);

            //存入数据集
            DataSet ds = new DataSet();
            da.Fill(ds);

            //打印数据
            DataTable dt = ds.Tables[0];
            info1.DataSource = dt.DefaultView;

            conn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String name = box2.Text.ToString();
            String strSql = @"select courseName, student.stuName, score
                                from student, course, courseTable
                                where course.courseName = '" + name + @"'
                                and course.courseId = courseTable.courseId
                                and courseTable.stuId = student.stuId
                                order by score asc;
                                select courseName, avg(score) as avgScore,count(*) as sumOfStudent
                                from student, course, courseTable
                                where course.courseName = '" + name + @"'
                                and course.courseId = courseTable.courseId
                                and courseTable.stuId = student.stuId
                                group by courseName";

            //创建连接
            if(conn.State == System.Data.ConnectionState.Closed)
                  conn.Open();

            //读取数据
            SqlDataAdapter da = new SqlDataAdapter(strSql, conn);

            //存入数据集
            DataSet ds = new DataSet();
            da.Fill(ds);

            //打印数据
            DataTable dt = ds.Tables[0];
            info2.DataSource = dt.DefaultView;

            dt = ds.Tables[1];
            info3.DataSource = dt.DefaultView;

            conn.Close();
        }


        DataTable dt3;
        private void button4_Click(object sender, EventArgs e)
        {
            String strSql = "select * from courseTable";

            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();


            //读取数据
            SqlDataAdapter da = new SqlDataAdapter(strSql, strConn);

            //放入数据集
            DataSet ds = new DataSet();
            da.Fill(ds);

            //打印数据
            dt3 = ds.Tables[0];
            info4.DataSource = dt3.DefaultView;

            //关闭连接
            conn.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {


            //增加,删除,修改
            //判断容器表是否为空
            if(dt3 == null)
            {
                button4_Click(sender, e);
                MessageBox.Show("无修改,默认为查询");
            }

            //判断是否有修改记录

            DataTable changeDt = null;
            try
            {
                changeDt = dt3.GetChanges();
            }catch(Exception ee)
            {
                MessageBox.Show("hello world !");
            }
            if(changeDt == null)
            {
                return;
            }

            //打开链接
            conn.Open();

            //获取修改 增加,删除,修改的记录

            foreach(DataRow dr in changeDt.Rows){
                string strSql = null;
                //增加
                if (dr.RowState == System.Data.DataRowState.Added)
                {
                    strSql = "insert into courseTable values("
                        +"'"+ dr["stuId"] + "','" + dr["courseId"] + "'," + dr["score"] + ")";
                }
                else if(dr.RowState == System.Data.DataRowState.Deleted)
                {
                    strSql = "delete from courseTable "
                            + "where stuId = '" + dr["stuId", DataRowVersion.Original] 
                            + "' and courseId = '"+ dr["courseId", DataRowVersion.Original] + "'";
                }
                else if(dr.RowState == System.Data.DataRowState.Modified)
                {
                    strSql = "update courseTable set"
                            + " courseId= '" + dr["courseId"]
                            + "',stuId = '" + dr["stuId"]
                            + "',score = " + dr["score"]
                           // + "',score = " + Convert.ToInt32(dr["score"]) int还是string对没有关系,最终都会转化为string
                            + " where stuId= '" + dr["stuId", DataRowVersion.Original]
                            + "' and courseId = '" + dr["courseId", DataRowVersion.Original] 
                            + "'; " 
                            + "update course set"
                            + " courseName = '操作系统' where courseId = '02'";
                }
                try
                {
                    SqlCommand comm = new SqlCommand(strSql, conn);
                    comm.ExecuteNonQuery();
                }catch (FormatException ee)
                {
                    MessageBox.Show("数据不符合数据库要求(类型,主键,外键)");
                }
        }




            //关闭连接
            conn.Close();


        }

        //下拉条
        private void tabPage1_Enter(object sender, EventArgs e)
        {
            string strSql = " select stuName from student";
            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();

            SqlDataAdapter da = new SqlDataAdapter(strSql, conn);

            DataSet ds = new DataSet();
            da.Fill(ds);

            DataTable dt = ds.Tables[0];

            comboBox1.Items.Clear();

            for(int i=0; i<dt.Rows.Count;i++)
            {
                comboBox1.Items.Add(dt.Rows[i][0]);
            }

            comboBox1.SelectedIndex = 0;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值