当前位置:首页 » 数据仓库 » 调取数据库信息用什么代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

调取数据库信息用什么代码

发布时间: 2022-05-23 11:13:59

A. 用jquery怎么从数据库中读取数据

jQuery是不能直接从数据库中读取数据的,只能是jQuery将需要查询或者想要读取的数据通过ajax等发送给后台编程语言php、Java等,由后台语言读取数据库后返回jQuery。

工具原料:编辑器

1、使用jQuery发送需要查询的数据给后台,然后获取后台返回的数据,简单的代码如下:

	<scripttype="text/javascript">
$.ajax({
type:"post",
url:"test.php",//发送的后台地址
data:{'name':'tom'},
success:function(data){
//此处处理后台返回的数据
}
});
</script>

2、一般是将后台的数据在按照需求现实在页面上。

B. php如何获取数据库信息

代码如下:?View
Code
PHP
include("conn.php");//调用数据库连接文件
echo
"<table
width=572
height=56
border=0
cellspacing=1>
";
//创建html表格
echo
"<tr
bgcolor=#9999FF>";
echo
"<th
width=33
scope=col>id</th>";
echo
"<th
width=100
scope=col>user_name</th>
";
echo
"<th
width=100
scope=col>user_pass</th>
";
echo
"<th
width=100
scope=col>staus</th>";
echo
"<th
width=100
scope=col>insert_time</th>";
echo
"</tr>";
$sql
=
"select
*
from
user_info";
$query
=
mysql_query($SQL);
//SQL查询语句
while
($row
=
mysql_fetch_array($query)){
//使用while循环mysql_fetch_array()并将数据返回数组
echo
"<tr
onmouseout=this.style.backgroundColor=''
onMouseOver=this.style.backgroundColor='#99CC33'
bgcolor=#CCCCCC>";
echo
"<td>$row[0]</td>";
//输出数组中数据
echo
"<td>$row[1]</td>";
echo
"<td>$row[2]</td>";
echo
"<td>$row[3]</td>";
echo
"<td>$row[4]</td>";
echo
"</tr>";
}
echo
"</table>";输出记录截图

C. oc读取mysql数据库数据完整代码

给你个例子供你参考

<spanstyle="font-size:14px;">#import"StudentDataBase.h"
#import"DB.h"
#import"StudentModel.h"
@implementationStudentDataBase

//查找所有数据
+(NSArray*)findAll{
sqlite3*db=[DBDBOpen];//调用DBopen方法获取数据库指针
sqlite3_stmt*stmt;//镜像出来的镜像不影响sqlite里的内容

intresult=sqlite3_prepare_v2(db,"select*fromstudent",-1,&stmt,Nil);
//-1是不限制字符串的长度&stmt是往stmt里面赋值Constchar**pzTail是设置前面哪个是不用的

NSMutableArray*peopels=[NSMutableArrayarray];//制作容器要装数据

//判断结果0进判断result==0表示正确
if(SQLITE_OK==result){

//判断是否有下一行
while(sqlite3_step(stmt)==SQLITE_ROW){
//intiCol是第几个
intstuId=sqlite3_column_int(stmt,0);//取一行中的一个int是值的类型
constunsignedcharchar*stuName=sqlite3_column_text(stmt,1);//取一行中的一个text是值的类型
constunsignedcharchar*stuSex=sqlite3_column_text(stmt,2);//取一行中的一个text是值的类型
floatstuScore=sqlite3_column_double(stmt,3);//取一行中的一个double是值的类型

//转换
NSString*currentName=[NSStringstringWithUTF8String:(constcharchar*)stuName];
NSString*currentSex=[NSStringstringWithUTF8String:(constcharchar*)stuSex];


StudentModel*student=[:stuIdname:currentNamesex:currentSexscore:stuScore];
//将model中内容添加到之前的容器数组
[peopelsaddObject:student];
}

sqlite3_finalize(stmt);//关闭镜像
returnpeopels;//将这个装有数据的数组返回
}else{
sqlite3_finalize(stmt);
return[NSArrayarray];//如果没进入if返回一个空数组
}

}

//查找单个数据
+(StudentModel*)findPeopleWith:(NSInteger)stuId{
sqlite3*db=[DBDBOpen];
sqlite3_stmt*stmt;
intresult=sqlite3_prepare_v2(db,"selectstuName,stuId,stuSex,stuScorefromstudentwherestuId=?",-1,&stmt,Nil);
sqlite3_bind_int(stmt,1,stuId);//第一个int是第几个问好?第二个int是绑定的内容绑定问好

if(SQLITE_OK==result){
if(sqlite3_step(stmt)==SQLITE_ROW){


constunsignedcharchar*stuName=sqlite3_column_text(stmt,0);
intstuId=sqlite3_column_int(stmt,1);
constunsignedcharchar*stuSex=sqlite3_column_text(stmt,2);
floatstuScore=sqlite3_column_double(stmt,3);


NSString*currentName=[NSStringstringWithUTF8String:(constcharchar*)stuName];
NSString*currentSex=[NSStringstringWithUTF8String:(constcharchar*)stuSex];

StudentModel*student=[:stuIdname:currentNamesex:currentSexscore:stuScore];
sqlite3_finalize(stmt);
NSLog(@"000");
returnstudent;
}else{
NSLog(@"11111");
returnnil;
}
}else{
NSLog(@"222222");
sqlite3_finalize(stmt);
returnnil;
}
}
//插入数据
+(void)insertIntoModel:(StudentModel*)model{
sqlite3*db=[DBDBOpen];
sqlite3_stmt*stmt;
intresult=sqlite3_prepare_v2(db,"insertintostudentvalues(?,?,?,?)",-1,&stmt,Nil);
sqlite3_bind_int(stmt,1,model.stuId);
sqlite3_bind_text(stmt,2,[model.stuNameUTF8String],-1,Nil);
sqlite3_bind_text(stmt,3,[model.stuSexUTF8String],-1,nil);
sqlite3_bind_double(stmt,4,model.stuScore);

if(SQLITE_OK==result){
if(sqlite3_step(stmt)==SQLITE_DONE){
NSLog(@"添加成功");
sqlite3_finalize(stmt);
return;
}else{
NSLog(@"添加失败");
sqlite3_finalize(stmt);
return;
}
}else{
NSLog(@"111");
sqlite3_finalize(stmt);

return;
}
}
//更新数据
+(void)updateStuName:(NSString*)namestuSex:(NSString*)sexstuScore:(CGFloat)scorestuId:(NSInteger)stuid{
sqlite3*db=[DBDBOpen];
sqlite3_stmt*stmt;
intresult=sqlite3_prepare_v2(db,"updatestudentsetstuName=?,stuSex=?,stuScore=?wherestuId=?",-1,&stmt,Nil);
sqlite3_bind_int(stmt,4,stuid);
sqlite3_bind_text(stmt,1,[nameUTF8String],-1,Nil);
sqlite3_bind_text(stmt,2,[sexUTF8String],-1,nil);
sqlite3_bind_double(stmt,3,score);
if(SQLITE_OK==result){
if(sqlite3_step(stmt)==SQLITE_DONE){
NSLog(@"更新成功");
sqlite3_finalize(stmt);
return;
}else{
NSLog(@"更新失败");
sqlite3_finalize(stmt);

}
}else{
NSLog(@"shi");
sqlite3_finalize(stmt);

}
}

//删除数据
+(void)deleteModelStuId:(NSInteger)stuid{
sqlite3*db=[DBDBOpen];
sqlite3_stmt*stmt;
intrelust=sqlite3_prepare_v2(db,"deletefromstudentwherestuid=?",-1,&stmt,nil);
sqlite3_bind_int(stmt,1,stuid);
if(SQLITE_OK==relust){
//step截段
if(sqlite3_step(stmt)==SQLITE_DONE){
NSLog(@"删除成功");
sqlite3_finalize(stmt);
return;
}else{
NSLog(@"删除失败");
sqlite3_finalize(stmt);
return;
}
}else{
NSLog(@"删除失败11");
sqlite3_finalize(stmt);
return;

}
}

@end
</span>

D. 请问c#如何调用数据库中的数据(具体的代码实现)以及sql中要做如何设置 谢谢!

sql中只要建立表即可,无需进行其他设置

  1. 在web.config中设置数据库连接字符串

<appSettings>

<add key="SqlConn" value="Server=服务器IP;DataBase=数据库名;UID=用户名;PWD=密码;"/>

</appSettings>

2.在登录页面登录按钮代码中加入语句

protected void LogButton_Click(object sender, EventArgs e)

{

string userid = this.Userid.Text.Trim();//用户名

string pwd = FormsAuthentication.(this.Pwd.Text.Trim(), "MD5");//密码

string ConStr = ConfigurationManager.AppSettings["SqlConn"].ToString();

SqlConnection sqlconn = new SqlConnection(ConStr);

sqlconn.Open();//建立连接

string mysql = "select count(*) as iCount from 表名 where UserID = '"+userid+"'";//查询语句

SqlCommand cmd = new SqlCommand(mysql, sqlconn);

SqlDataReader sqlreader = cmd.ExecuteReader();

sqlreader.Read();//查询表数据

string Count = sqlreader["iCount"].ToString();

sqlreader.Close();

sqlconn.Close();

if (Count != "0")

{

sqlconn.Open();

string mysql1 = "select * from 表名 where UserID = '" + userid + "'";

SqlCommand cmd1 = new SqlCommand(mysql1, sqlconn);

SqlDataReader sqlreader1 = cmd1.ExecuteReader();

sqlreader1.Read();

string DrPwd = sqlreader1["UserPwd"].ToString().Trim();

string DrUser = sqlreader1["UserName"].ToString().Trim();

sqlreader1.Close();

sqlconn.Close();

if (DrPwd == pwd)

{

Session["logname"] = DrUser;

Session["logstate"] = 1;

Response.Redirect("main.aspx"); //登录下级界面

}

else

{

Response.Write("登录密码错误!");

this.Pwd.Focus();

}

}

else

{

Response.Write("登录用户错误!");

this.Userid.Focus();

}

}

你可以参考以上登录代码,自行编辑查询语句,实现调用数据库数据的功能.望采纳,谢谢.

E. 如何用php获取数据库信息并显示

获取ppq数据库的所有表名的代码:
?php
$server='localhost';
$user='root';
$pass='12345';
$dbname='ppq';
$conn=mysql_connect($server,$user,$pass);
if(!$conn)
die("数据库系统连接失败!");
$result=mysql_list_tables($dbname);
if(!$result)
die("数据库连接失败!");
while($row=mysql_fetch_row($result))
{
echo
$row[0]."
";
}
mysql_free_result($result);
?
mysql_list_tables
(PHP
3,
PHP
4
,
PHP
5)
mysql_list_tables
--
列出
MySQL
数据库中的表
说明
resource
mysql_list_tables
(
string
database
[,
resource
link_identifier])
mysql_list_tables()
接受一个数据库名并返回和
mysql_query()
函数很相似的一个结果指针。用
mysql_fetch_array()或者用mysql_fetch_row()来获得一个数组,数组的第0列就是数组名,当获取不到时
mysql_fetch_array()或者用mysql_fetch_row()返回
FALSE。

F. 急求一个VB调用数据库的程序代码!!

---- VB做为快速应用开发(RAD)工具越来越得到开发人员的认可和接受。它对许多API(如ODBC API、SOCKET API等等)的封装使得编程变得简单起来。同时,它支持集成开发环境下的可视化、事件驱动、面向对象等编程特点。下面,我们谈谈在VB中调用存储过程的实现方法及其注意事项。 ---- 我们知道,VB的数据库编程有许多种方法,比如直接用ODBC API编程,这种方法灵活、高效,程序员可以实现对数据库复杂的控制;也可以用VB中的数据对象,如RDO(远程数据对象)、DAO(数据访问对象)、ADO(ActiveX 数据对象),这种方法实现起来方便、快捷,但灵活性较差一些。由于存储过程在实现数据封装、隐藏以及代码的预编译、减少网络负载、维护方便等优点,所以被许多RDBMS和编程工具做支持。VB中的各类数据对象也提供对存储过程的支持。 ---- 我们以ADO为例来说明其实现的步骤 ---- 1. 创建、调试存储过程。你可以在数据库中也可以在其他外挂程序的支持下进行存储过程的创建和调试工作。本例中的存储过程代码如下(使用PUBS的MS SQL中的例子库 ): CREATE PROCEDURE myprocere @job_id smallint, @job_lvl tinyint AS SELECT * FROM employee WHERE job_id < @job_id AND job_lvl > @job_lvl ---- 2. 在VB中生成一个新的工程,工程有一窗体,一个COMMAND(NAME:COMMAND1) 按钮,一个 MSFlexGrid(NAME:MSFlexGrid1)控件。 ---- 3. 创建连接ADO connection; ---- 4. 创建命令ADO command; ---- 5. 创建参数并设置各个参数的属性; ---- 6. 执行ADO command; ---- 7. 对数据进行处理;MSFlexGrid显示查询到的数据 ---- 8. 释放连接,退出程序。 ---- 其中代码如下: 在窗体中声明以下变量: Dim cnn1 As ADODB.Connection ‘连接 Dim mycommand As ADODB.Command ‘命令 Dim parm_jobid As ADODB.Parameter ‘参数1 Dim parm_joblvl As ADODB.Parameter ‘参数2 Dim rstByQuery As ADODB.Recordset ‘结果集 Dim strCnn As String ‘连接字符串 在窗体的LOAD事件中加入如下代码: Set cnn1 = New ADODB.Connection ‘生成一个连接 strCnn = "DSN=MYDSN;uid=sa;pwd=" ‘创建的系统数据源MYDSN指向PUBS数据库 cnn1.Open strCnn ‘打开连接 在窗体的UNLOAD中的加入代码如下: cnn1.Close ‘关闭连接 Set cnn1 = Nothing ‘释放连接 在按钮中的代码如下: Dim i As integer Dim j as integer Set parm_jobid = New ADODB.Parameter Set mycommand = New ADODB.Command ' parm_jobid.Name = "name1" this line can be ommited parm_jobid.Type = adInteger ‘

G. 用Java怎样访问数据库,用什么代码

1. 加载一个对应数据库的JDBC驱动

在建立到一个数据库的连接之前,必须先加载这个数据库的JDBC驱动程序,加载之后此driver会自动注册到JDBC驱动列表中。加载一个JDBC驱动有两种方法。

a) 在命令行方式下指定驱动器或者用冒号分割驱动器列表:

具体命令如下:

C:\>java –Djdbc.drivers = com.company1.Driver:com.company2.Driver youProject

b)第二种方法,在程序中调用Class.forName()方法。推荐使用。。。。

try

{

String driverName = “com.imaginary.sql.msql.MsqlDriver”;

Class.forName(driverName).newInstance();

}

Catch(ClassNotFoundException e1)

{

//catch could not find database driver exception.

}

2.连接到数据库。

根据您后台待连接的数据库不同,而有小小的差别。

a) 连接到Oracle数据库。

Connection connection = null ;

try

{

//load the jdbc driver ;

String driverName = “oracle.jdbc.driver.OracleDriver”;

Class.forName(driverName).newInstance();

//create a connection to the database;

String serverName = “127.0.0.1”;

String serverPort = “1521”;

String serverID = “datebase1”

String userName = “hello”;

String userPsw = “world”;

String url = “jdbc:oracle.thin:@” + serverName + “:” + serverPort + “:” + serverID ;

Connection = DriverManager.getConnection(url , userName , userPsw);

}

catch(ClassNotFoundException e1)

{

//catch could not find database driver exception.

}

catch(SQLException e2)

{

//catch could not connect to the database exception.

}

b) 连接到一个SQL Server数据库。

Connection connection = null ;

try

{

//load the jdbc driver ;

String driverName = “com.microsoft.jdbc.sqlserver.SQLServerDriver”;

Class.forName(driverName).newInstance();

//create a connection to the database;

String serverName = “127.0.0.1”;

String serverPort = “1433”;

String serverID = serverName + serverPort ;

String userName = “hello”;

String userPsw = “world”;

String url = “jdbc:JSQLConnect ://” + serverID ;

Connection = DriverManager.getConnection(url , userName , userPsw);

}

catch(ClassNotFoundException e1)

{

//catch could not find database driver exception.

}

catch(SQLException e2)

{

//catch could not connect to the database exception.

}

c) 连接到一个MySQL数据库上。。。。

Connection connection = null ;

try

{

//load the jdbc driver ;

String driverName = “org.gjt.mm.mysql.Driver”;

Class.forName(driverName).newInstance();

//create a connection to the database;

String serverName = “127.0.0.1”;

String serverID = “database”;

String userName = “hello”;

String userPsw = “world”;

String url = “jdbc:mysql ://” + serverName + “/” + serverID ;

Connection = DriverManager.getConnection(url , userName , userPsw);

}

catch(ClassNotFoundException e1)

{

//catch could not find database driver exception.

}

catch(SQLException e2)

{

//catch could not connect to the database exception.

}

综合上面的三种数据库连接方式 , 其实大同小异。由于访问不同的数据库和所使用的数据库驱动程序不同,所以导致代码表面上有小小不同,但透过表面看来,内部都是

1. 加载一个特定的数据库JDBC驱动。

2. 连接到一个数据库。

3. 之后,就可以对一个特定的数据库进行特定的操作了。

附上各种数据库的JDBC驱动起可用信息网址:

http://java.sun.com/procts/jdbc

对于Oracle数据库,请参考:

http://otn.oracle.com.software/content.html

对于MySQL数据库,请参考:

http://mmMySQL.sourceforge.net

对于SQL Server数据库,有很多的驱动可选,比较常用的:

http://www.microsoft.com/china/sql/downloads/2000/jdbc.asp

http://www.freetds.org

http://www.datadirect-technologies.com

H. VB中,我想用data控件读取一个数据库,需要写什么代码

可以不用写代码,直接用data控件的属性设置,将其与数据库相连后,选择想要查询的数据表作为数据源。再根据你要显示在窗体上的内容用相关控件显示出来。也可能用Datagrid控与Data控件相连来显示。至于显示下一个,那就要用一下代码。采用数据库游标来读取。

I. 如何使用eclipse调用数据库中的内容,并将数据显示出来具体步骤和代码··

我用的数据库是mysql,下载这个东东mysql-connector-java-5.1.15.zip解压把mysql-connector-java-5.1.15-bin.jar导入到你要连接数据库的项目中(应该知道怎么导入吧!)然后就是代码,以下代码是插入数据库的例子

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Login2 {

private Connection connection;
private JButton button1;
private JFrame frame;
private JLabel nameLabel,pwdLabel;
private JTextField nameTA,pwdTA;
private JPanel panel;
// private Statement stat;
private ResultSet rs;
public Login2()
{
String url = "jdbc:mysql://localhost:3306/(此处填写你创建的数据库名字)";
String username = "(此处填写你的数据库用户,例如root)";
String password = "(此处填写你的数据库安装时设置的密码)";
//加载驱动程序以连接数据库
try {
Class.forName( "org.gjt.mm.mysql.Driver" );
connection = DriverManager.getConnection(
url, username, password );
}
//捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"装载 JDBC/ODBC 驱动程序失败。" );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
//捕获连接数据库异常
catch ( SQLException sqlex ) {
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
frame = new JFrame();

panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
nameLabel = new JLabel("user");
pwdLabel = new JLabel("password");
nameTA = new JTextField();
pwdTA = new JTextField();
button1 = new JButton("insert");

button1.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
String str1 = nameTA.getText();
String str2 = pwdTA.getText();
String str = "insert into user values('"+str1+"','"+str2+"')";

try {
// Statement stat = null;
PreparedStatement pstmt = connection.prepareStatement(str);

pstmt.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "yes");

}
});

panel.add(nameLabel);
panel.add(nameTA);
panel.add(pwdLabel);
panel.add(pwdTA);
panel.add(button1);

frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);

}

/**
* @param args
*/
public static void main(String[] args) {

Login2 l = new Login2();

}

}
还有问题可以继续联系