當前位置:首頁 » 數據倉庫 » 調取資料庫信息用什麼代碼
擴展閱讀
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();

}

}
還有問題可以繼續聯系