Android studio连接SQLserver数据库动态查询输出课程表
效果如下:
整体采用蓝色调,在设计课程表的时候一直在考虑选取什么样的布局比较好,本来想选择网格布局,然而网格布局对课程表的跨行显示太不友好了,在经过反复思考,辗转反侧之后,突然想到了相对布局,没错就是相对布局,没想到吧,嘿嘿。
相对布局的话,原理就变得简单起来,只把每节课程看作是一个固定宽度固定高度的文本控件TextView就可以了,然后只要根据上课时间,根据距top的距离和距left的距离插入进去就可以啦,真的是非常不错。
代码
// java文件
package com.example.xuanke;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class CourseActivity extends Activity {
//星期几
RelativeLayout day;
View bj;
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:jtds:sqlserver://10.95.222.000:1433;DatabaseName=school";
//String dbURL=getResources().getString(R.string.xx);//数据库连接url
String userName="sa";//数据库用户名
String userPwd="";//数据库密码
String u;
Connection con;
int currentCoursesNumber = 0;
int maxCoursesNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
Intent intent = getIntent();
u = intent.getStringExtra("username");
bj=findViewById(R.id.bj);
bj.getBackground().setAlpha(100);
//从数据库读取数据
loadData();
}
//从数据库加载数据
private void loadData() {
String date = getResources().getString(R.string.user);
System.out.println(date);
new Thread() {
public void run() {
try {
Class.forName(driverName);
//通过URL、用户名和用户密码连接数据库
con = DriverManager.getConnection(dbURL, userName, userPwd);
//查询语句,查询该老师开设的课程的课程名称、星期、时间、时长、上课地点
String sql = "SELECT name,wkd,sksj,sc,dd FROM Course where tid='"+u+"'";
//连接数据库并获取查询结果
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
//定义变量
String name = "";
String dd = "";
int da;
int start;
int time;
//查询结果可能有多条信息,通过while循环输出
while (rs.next()) {
//通过查询的列名,分别获取查询信息。
name = rs.getString("name");
dd = rs.getString("dd");
da = rs.getInt("wkd");
start = rs.getInt("sksj");
time = rs.getInt("sc");
//创建"第几节数"视图
for (int i = 0; i <13; i++) {
//创建13节课的左侧边栏
//将left_view.xml文件中设计界面变成一个可以使用的View
View view = LayoutInflater.from(CourseActivity.this).inflate(R.layout.left_view, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(110,130);
view.setLayoutParams(params);
TextView text = view.findViewById(R.id.class_number_text);
text.setText(String.valueOf(++currentCoursesNumber));
LinearLayout leftViewLayout = findViewById(R.id.left_view_layout);
leftViewLayout.addView(view);
}
//创建单个课程视图
int getDay = da;
int dayId = 0;
//获取上课日的id
switch (getDay) {
case 1:
dayId = R.id.monday;
break;
case 2:
dayId = R.id.tuesday;
break;
case 3:
dayId = R.id.wednesday;
break;
case 4:
dayId = R.id.thursday;
break;
case 5:
dayId = R.id.friday;
break;
case 6:
dayId = R.id.saturday;
break;
case 7:
dayId = R.id.weekday;
break;
}
day = findViewById(dayId);
//height设置为高度的基本单位,即一节课的高度
int height = 130;
//加载单个课程布局
View v = LayoutInflater.from(CourseActivity.this).inflate(R.layout.course_card, null);
v.setY(height * (start - 1)); //设置开始高度,即第几节课开始
//设置布局高度,即跨多少节课
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (time) * height - 8);
v.setLayoutParams(params2);
TextView text2 = v.findViewById(R.id.text_view);
text2.setText(name+dd); //显示课程名
//在该列加入该TextView
day.addView(v);
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
;
}
}
}.start();
}
}
// xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bj"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg3"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--星期条-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:alpha="1"
android:background="#7F126FB4">
<TextView
android:layout_width="110px"
android:layout_height="match_parent"
android:gravity="center"
android:text="节/周"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="周一"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="周二"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="周三"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="周四"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="周五"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="周六"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="周日"/>
</LinearLayout>
<!--课程表-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--左侧"节数"布局-->
<LinearLayout
android:id="@+id/left_view_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<!--星期一到星期天的布局-->
<RelativeLayout
android:id="@+id/monday"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_margin="1dp"/>
<RelativeLayout
android:id="@+id/tuesday"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_margin="1dp"/>
<RelativeLayout
android:id="@+id/wednesday"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_margin="1dp"/>
<RelativeLayout
android:id="@+id/thursday"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_margin="1dp"/>
<RelativeLayout
android:id="@+id/friday"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_margin="1dp"/>
<RelativeLayout
android:id="@+id/saturday"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_margin="1dp"/>
<RelativeLayout
android:id="@+id/weekday"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginTop="1dp"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>