
DbConnect
db
=
DbConnect
getInstance
();
这是一个连接数据库的类(你自己写的),初始化
db相当一个别名,可以点出()该类里面的方法
Connection
conn
=
dbgetConnect();
dbgetConnect();这个方法
应该是你
写的一个连接方法了,返回的是Connection
类型,所以用他接收
此时已经连接的数据库了。接下来你要对数据库做什么····
String
sql="select
from
(select
from
notice_hanchao
order
by
insert_time
desc)";
一条sql查询语句
PreparedStatement
ps
=
connprepareStatement(sql);
connprepareStatement(sql);数据库对
sql语句
判断和执行,返回的是PreparedStatement
所以用他接收
ResultSet
rs
=
psexecuteQuery();
执行sql语句后要接收吧,psexecuteQuery();返回一个
结果集
,用ResultSet
去接收
接下来你可以打印了
rsgetString(1);
rsget类型(1);
rsgetString(2);
rsget类型(2);········
不懂就去问你们老师吧···我只能这么写了··希望能帮助你
给select一个name,然后对每个option 给一个value值,点击提交后,提交到后台的是一个数组,写给你看吧
<form action='' method='post'><selectg name='table'>
<option value='all' selected>请选择</option>
<option value='name'>name</option>
</select>
<input type='submit' name='sub' value='提交'/>
</form>
//php代码
<php
if(isset($_POST['sub'])){
print_r($_POST);
//现在打印这个数组就会发现有一个 table 指向你选择的内容;只需判断table等于什么就可以了,而这个值就取决于上面的option里面的value
if($_POST['table'] == 'all'){
//查询整张表
}elseif($_POST['table'] == 'name'){
//按条件查询
}
//当你有多个条件的时候,这里还可以继续elseif
}
>
新建一个数据集-----连接数据库!---绑定表格-----Dataset1xsd
private void Form1_Load(object sender, SystemEventArgs e)
{
for(int i = dataGrid1ControlsCount -1;i>=0;i--)//循环表格
{
Control ctr = dataGrid1Controls[i];
if(ctr is DataGridTextBox)//删除表格中的单元格
{
dataGrid1ControlsRemoveAt(i);//删除
}
}
sqlDataAdapter1Fill(thisdataset11Flight);
}
private Dataset1FlightRow currentRow;
private void dataGrid1_CurrentCellChanged(object sender, SystemEventArgs e)//表格改变事件CurrentCellChanged
{
thisdataGrid1Select(dataGrid1CurrentRowIndex);//选中整行
DataRowView view=(DataRowView)thisBindingContext//绑定数据行视图[dataset11,"Flight"]Current;
currentRow=(Dataset1FlightRow)viewRow;
thistxtcodeText=currentRowFlightCode;//选中的行在文本框中显示
thistxtstartText=currentRowSource;
thistxtoverText=currentRowDestination;
}
--------------------完整的例子--------------------------------------
using System;
using SystemDrawing;
using SystemCollections;
using SystemComponentModel;
using SystemWindowsForms;
using SystemData;
namespace Flight
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : SystemWindowsFormsForm
{
private SystemWindowsFormsDataGrid dataGrid1;
private SystemWindowsFormsTextBox txtcode;
private SystemWindowsFormsTextBox txtstart;
private SystemWindowsFormsTextBox txtover;
private SystemWindowsFormsButton button1;
private SystemWindowsFormsButton button2;
private SystemWindowsFormsButton button3;
private SystemWindowsFormsButton button4;
private SystemDataSqlClientSqlDataAdapter sqlDataAdapter1;
private SystemDataSqlClientSqlCommand sqlSelectCommand1;
private SystemDataSqlClientSqlCommand sqlInsertCommand1;
private SystemDataSqlClientSqlCommand sqlUpdateCommand1;
private SystemDataSqlClientSqlCommand sqlDeleteCommand1;
private SystemDataSqlClientSqlConnection conn;
private FlightDataset1 dataset11;
private SystemWindowsFormsDataGridTableStyle dataGridTableStyle1;
private SystemWindowsFormsDataGridTextBoxColumn dataGridTextBoxColumn1;
private SystemWindowsFormsDataGridTextBoxColumn dataGridTextBoxColumn2;
private SystemWindowsFormsDataGridTextBoxColumn dataGridTextBoxColumn4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private SystemComponentModelContainer components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
componentsDispose();
}
}
baseDispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
thisdataGrid1 = new SystemWindowsFormsDataGrid();
thisdataset11 = new FlightDataset1();
thisdataGridTableStyle1 = new SystemWindowsFormsDataGridTableStyle();
thisdataGridTextBoxColumn1 = new SystemWindowsFormsDataGridTextBoxColumn();
thisdataGridTextBoxColumn2 = new SystemWindowsFormsDataGridTextBoxColumn();
thisdataGridTextBoxColumn4 = new SystemWindowsFormsDataGridTextBoxColumn();
thistxtcode = new SystemWindowsFormsTextBox();
thistxtstart = new SystemWindowsFormsTextBox();
thistxtover = new SystemWindowsFormsTextBox();
thisbutton1 = new SystemWindowsFormsButton();
thisbutton2 = new SystemWindowsFormsButton();
thisbutton3 = new SystemWindowsFormsButton();
thisbutton4 = new SystemWindowsFormsButton();
thissqlDataAdapter1 = new SystemDataSqlClientSqlDataAdapter();
thissqlDeleteCommand1 = new SystemDataSqlClientSqlCommand();
thisconn = new SystemDataSqlClientSqlConnection();
thissqlInsertCommand1 = new SystemDataSqlClientSqlCommand();
thissqlSelectCommand1 = new SystemDataSqlClientSqlCommand();
thissqlUpdateCommand1 = new SystemDataSqlClientSqlCommand();
((SystemComponentModelISupportInitialize)(thisdataGrid1))BeginInit();
((SystemComponentModelISupportInitialize)(thisdataset11))BeginInit();
thisSuspendLayout();
//
// dataGrid1
//
thisdataGrid1DataMember = "Flight";
thisdataGrid1DataSource = thisdataset11;
thisdataGrid1HeaderForeColor = SystemDrawingSystemColorsControlText;
thisdataGrid1Location = new SystemDrawingPoint(8, 8);
thisdataGrid1Name = "dataGrid1";
thisdataGrid1ReadOnly = true;
thisdataGrid1SelectionForeColor = SystemDrawingColorBrown;
thisdataGrid1Size = new SystemDrawingSize(312, 208);
thisdataGrid1TabIndex = 0;
thisdataGrid1TableStylesAddRange(new SystemWindowsFormsDataGridTableStyle[] {
thisdataGridTableStyle1});
thisdataGrid1Navigate += new SystemWindowsFormsNavigateEventHandler(thisdataGrid1_Navigate);
thisdataGrid1CurrentCellChanged += new SystemEventHandler(thisdataGrid1_CurrentCellChanged);
//
// dataset11
//
thisdataset11DataSetName = "Dataset1";
thisdataset11Locale = new SystemGlobalizationCultureInfo("en-US");
//
// dataGridTableStyle1
//
thisdataGridTableStyle1DataGrid = thisdataGrid1;
thisdataGridTableStyle1GridColumnStylesAddRange(new SystemWindowsFormsDataGridColumnStyle[] {
thisdataGridTextBoxColumn1,
thisdataGridTextBoxColumn2,
thisdataGridTextBoxColumn4});
thisdataGridTableStyle1HeaderForeColor = SystemDrawingSystemColorsControlText;
thisdataGridTableStyle1MappingName = "Flight";
//
// dataGridTextBoxColumn1
//
thisdataGridTextBoxColumn1Format = "";
thisdataGridTextBoxColumn1FormatInfo = null;
thisdataGridTextBoxColumn1HeaderText = "航班号";
thisdataGridTextBoxColumn1MappingName = "FlightCode";
thisdataGridTextBoxColumn1Width = 75;
//
// dataGridTextBoxColumn2
//
thisdataGridTextBoxColumn2Format = "";
thisdataGridTextBoxColumn2FormatInfo = null;
thisdataGridTextBoxColumn2HeaderText = "起点";
thisdataGridTextBoxColumn2MappingName = "Source";
thisdataGridTextBoxColumn2Width = 75;
//
// dataGridTextBoxColumn4
//
thisdataGridTextBoxColumn4Format = "";
thisdataGridTextBoxColumn4FormatInfo = null;
thisdataGridTextBoxColumn4HeaderText = "终点";
thisdataGridTextBoxColumn4MappingName = "Destination";
thisdataGridTextBoxColumn4Width = 75;
//
// txtcode
//
thistxtcodeLocation = new SystemDrawingPoint(336, 24);
thistxtcodeName = "txtcode";
thistxtcodeSize = new SystemDrawingSize(168, 21);
thistxtcodeTabIndex = 1;
thistxtcodeText = "";
//
// txtstart
//
thistxtstartLocation = new SystemDrawingPoint(336, 72);
thistxtstartName = "txtstart";
thistxtstartSize = new SystemDrawingSize(168, 21);
thistxtstartTabIndex = 2;
thistxtstartText = "";
//
// txtover
//
thistxtoverLocation = new SystemDrawingPoint(336, 120);
thistxtoverName = "txtover";
thistxtoverSize = new SystemDrawingSize(168, 21);
thistxtoverTabIndex = 3;
thistxtoverText = "";
//
// button1
//
thisbutton1Location = new SystemDrawingPoint(344, 152);
thisbutton1Name = "button1";
thisbutton1TabIndex = 4;
thisbutton1Text = "添加";
thisbutton1Click += new SystemEventHandler(thisbutton1_Click);
//
// button2
//
thisbutton2Location = new SystemDrawingPoint(344, 192);
thisbutton2Name = "button2";
thisbutton2TabIndex = 5;
thisbutton2Text = "删除";
thisbutton2Click += new SystemEventHandler(thisbutton2_Click);
//
// button3
//
thisbutton3Location = new SystemDrawingPoint(432, 152);
thisbutton3Name = "button3";
thisbutton3TabIndex = 6;
thisbutton3Text = "修改";
thisbutton3Click += new SystemEventHandler(thisbutton3_Click);
//
// button4
//
thisbutton4Location = new SystemDrawingPoint(432, 192);
thisbutton4Name = "button4";
thisbutton4TabIndex = 5;
thisbutton4Text = "退出";
thisbutton4Click += new SystemEventHandler(thisbutton4_Click);
//
// sqlDataAdapter1
//
thissqlDataAdapter1DeleteCommand = thissqlDeleteCommand1;
thissqlDataAdapter1InsertCommand = thissqlInsertCommand1;
thissqlDataAdapter1SelectCommand = thissqlSelectCommand1;
thissqlDataAdapter1TableMappingsAddRange(new SystemDataCommonDataTableMapping[] {
new SystemDataCommonDataTableMapping("Table", "Flight", new SystemDataCommonDataColumnMapping[] {
new SystemDataCommonDataColumnMapping("FlightCode", "FlightCode"),
new SystemDataCommonDataColumnMapping("Source", "Source"),
new SystemDataCommonDataColumnMapping("Destination", "Destination")})});
thissqlDataAdapter1UpdateCommand = thissqlUpdateCommand1;
//
// sqlDeleteCommand1
//
thissqlDeleteCommand1CommandText = "DELETE FROM Flight WHERE (FlightCode = @Original_FlightCode) AND (Destination = @" +
"Original_Destination OR @Original_Destination IS NULL AND Destination IS NULL) A" +
"ND (Source = @Original_Source OR @Original_Source IS NULL AND Source IS NULL)";
thissqlDeleteCommand1Connection = thisconn;
thissqlDeleteCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_FlightCode", SystemDataSqlDbTypeVarChar, 10, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "FlightCode", SystemDataDataRowVersionOriginal, null));
thissqlDeleteCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_Destination", SystemDataSqlDbTypeVarChar, 50, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "Destination", SystemDataDataRowVersionOriginal, null));
thissqlDeleteCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_Source", SystemDataSqlDbTypeVarChar, 50, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "Source", SystemDataDataRowVersionOriginal, null));
//
// conn
//
thisconnConnectionString = "workstation id=PC210;packet size=4096;integrated security=SSPI;initial catalog=Ai" +
"r;persist security info=False";
//
// sqlInsertCommand1
//
thissqlInsertCommand1CommandText = "INSERT INTO Flight(FlightCode, Source, Destination) VALUES (@FlightCode, @Source," +
" @Destination); SELECT FlightCode, Source, Destination FROM Flight WHERE (Flight" +
"Code = @FlightCode)";
thissqlInsertCommand1Connection = thisconn;
thissqlInsertCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@FlightCode", SystemDataSqlDbTypeVarChar, 10, "FlightCode"));
thissqlInsertCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Source", SystemDataSqlDbTypeVarChar, 50, "Source"));
thissqlInsertCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Destination", SystemDataSqlDbTypeVarChar, 50, "Destination"));
//
// sqlSelectCommand1
//
thissqlSelectCommand1CommandText = "SELECT FlightCode, Source, Destination FROM Flight";
thissqlSelectCommand1Connection = thisconn;
//
// sqlUpdateCommand1
//
thissqlUpdateCommand1CommandText = @"UPDATE Flight SET FlightCode = @FlightCode, Source = @Source, Destination = @Destination WHERE (FlightCode = @Original_FlightCode) AND (Destination = @Original_Destination OR @Original_Destination IS NULL AND Destination IS NULL) AND (Source = @Original_Source OR @Original_Source IS NULL AND Source IS NULL); SELECT FlightCode, Source, Destination FROM Flight WHERE (FlightCode = @FlightCode)";
thissqlUpdateCommand1Connection = thisconn;
thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@FlightCode", SystemDataSqlDbTypeVarChar, 10, "FlightCode"));
thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Source", SystemDataSqlDbTypeVarChar, 50, "Source"));
thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Destination", SystemDataSqlDbTypeVarChar, 50, "Destination"));
thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_FlightCode", SystemDataSqlDbTypeVarChar, 10, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "FlightCode", SystemDataDataRowVersionOriginal, null));
thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_Destination", SystemDataSqlDbTypeVarChar, 50, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "Destination", SystemDataDataRowVersionOriginal, null));
thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_Source", SystemDataSqlDbTypeVarChar, 50, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "Source", SystemDataDataRowVersionOriginal, null));
//
// Form1
//
thisAutoScaleBaseSize = new SystemDrawingSize(6, 14);
thisClientSize = new SystemDrawingSize(528, 229);
thisControlsAdd(thisbutton3);
thisControlsAdd(thisbutton2);
thisControlsAdd(thisbutton1);
thisControlsAdd(thistxtover);
thisControlsAdd(thistxtstart);
thisControlsAdd(thistxtcode);
thisControlsAdd(thisdataGrid1);
thisControlsAdd(thisbutton4);
thisName = "Form1";
thisText = "Form1";
thisLoad += new SystemEventHandler(thisForm1_Load);
((SystemComponentModelISupportInitialize)(thisdataGrid1))EndInit();
((SystemComponentModelISupportInitialize)(thisdataset11))EndInit();
thisResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
ApplicationRun(new Form1());
}
private void button4_Click(object sender, SystemEventArgs e)
{
ApplicationExit();
}
private void Form1_Load(object sender, SystemEventArgs e)
{
for(int i = dataGrid1ControlsCount -1;i>=0;i--)
{
Control ctr = dataGrid1Controls[i];
if(ctr is DataGridTextBox)
{
dataGrid1ControlsRemoveAt(i);
}
}
sqlDataAdapter1Fill(thisdataset11Flight);
}
private Dataset1FlightRow currentRow;
private void dataGrid1_CurrentCellChanged(object sender, SystemEventArgs e)
{
thisdataGrid1Select(dataGrid1CurrentRowIndex);//选中整行
DataRowView view=(DataRowView)thisBindingContext[dataset11,"Flight"]Current;
currentRow=(Dataset1FlightRow)viewRow;
thistxtcodeText=currentRowFlightCode;
thistxtstartText=currentRowSource;
thistxtoverText=currentRowDestination;
}
private void button3_Click(object sender, SystemEventArgs e)
{
currentRowFlightCode=thistxtcodeText;
currentRowSource=thistxtstartText;
currentRowDestination= thistxtoverText;
sqlDataAdapter1Update(thisdataset11Flight);
}
private void button1_Click(object sender, SystemEventArgs e)
{
DataRow newrow = dataset11Tables[0]NewRow();
newrow[0]=thistxtcodeText;
newrow[1]=thistxtstartText;
newrow[2]=thistxtoverText;
dataset11FlightRowsAdd(newrow);
sqlDataAdapter1Update(thisdataset11Flight);
}
private void button2_Click(object sender, SystemEventArgs e)
{
currentRowDelete();
sqlDataAdapter1Update(thisdataset11Flight);
MessageBoxShow("删除成功!!!");
}
private void dataGrid1_Navigate(object sender, SystemWindowsFormsNavigateEventArgs ne)
{
}
}
}
----------------------------数据库--------------------------------
create database Air
go
use Air
go
create table Flight
(
FlightCode varchar(10) primary key,
Source varchar(50),
Destination varchar(50)
)
go
insert into Flight values('AF0001','厦门','上海')
insert into Flight values('BH8888','厦门','北京')
insert into Flight values('DR8988','北京','上海')
insert into Flight values('UI6766','福州','上海')
insert into Flight values('FG5432','北京','西安')
insert into Flight values('QW4555','兰州','上海')
insert into Flight values('ES4333','北京','成都')
insert into Flight values('NB5666','南昌','上海')
select from Flight
-----------------------还要建设一个数据集----------------
选择项目---添加新建项-----数据集(不要改名)---从服务器资源管理器(左边的数据库)把上面的表格拉到里面搞定!!!
联合查询
sql="select from [cp1] where bh like '%"&mc&"%'or mc like '%"&mc&"%' or ty like '%"&mc&"%'or sy like '%"&mc&"%'order by ID desc union select from [cp] where mc like '%"&mc&"%' order by ID desc"
不过要求两张表的结构要是相同的
select 特殊的几个 from table where 条件 union all
(select 其他的几个 from table where 条件 order by sort_id)
---------------补充-----------------
排到第三或第四的话 ,那你就头两条先按你原来的排序实现,中间union all那几个特殊的
最后排除掉头两条,剩余的再union all,不过这样似乎是麻烦点了,不过也没有太好的办法
不知道你具体是些什么样的数据,如果单纯这么说的话我怕把你绕进去,不如你发点数据,按照数据描述一下你的意思吧
两种方法
执行后会返回一个结果集对象ResultSet rs
第一种
while(rsnext()){
rsgetString("列名")
}
第二种
while(rsnext()){
rsgetString(1)//1是下标 表示查询出来的数据的第一列
}
HHH = Range("A5") ’--这里 HHH本身就是一个 Range对象,代表的就是 Rang("A5")
所以 Range(HHH)Select 换成 HHHSelect 应该就可以了
以上就是关于JAVA 关于调用数据库全部的内容,包括:JAVA 关于调用数据库、如何实现select下拉菜单 查询数据库 谢谢 ~、调用数据库的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)