首先你要在項(xiàng)目中加載對(duì)應(yīng)數(shù)據(jù)庫(kù)的驅(qū)動(dòng)包,然后進(jìn)行數(shù)據(jù)庫(kù)查詢,代碼待會(huì)給你。
成都創(chuàng)新互聯(lián)主營(yíng)廣南網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都APP應(yīng)用開發(fā),廣南h5微信小程序開發(fā)搭建,廣南網(wǎng)站營(yíng)銷推廣歡迎廣南等地區(qū)企業(yè)咨詢
String sql="";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc.mysql//localhost:3306/你的數(shù)據(jù)庫(kù)名","username","pwd");
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getInt(2));
}
HTML:
form action="insert.php" method="post"
Firstname: input type="text" name="firstname" /
Lastname: input type="text" name="lastname" /
Age: input type="text" name="age" /
input type="submit" /
/form
?php
$con = mysql_connect("localhost:3306","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM user");
echo "table border='1'
tr
thUsername/th
thPassword/th
/tr";
while($row = mysql_fetch_array($result)) {
echo "tr";
echo "td" . $row['username'] . "/td";
echo "td" . $row['password'] . "/td";
echo "/tr";
}
echo "/table";
mysql_close($con);
?
從服務(wù)器中獲取用戶所有信息(SQL SELECT語(yǔ)句)并以表格形式出現(xiàn)
?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("DELETE FROM user WHERE username = '$_POST[username]'");
mysql_close($con);
?
刪除該用戶所有信息delete.php
?php
$con = mysql_connect("localhost:3306","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql = "INSERT INTO user (username,password)
VALUES
('$_POST[username]','$_POST[password]')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?
注冊(cè)一個(gè)新用戶insert.php
?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("UPDATE user SET password = '$_POST[password]' WHERE username = '$_POST[username]'");
mysql_close($con);
?
修改一個(gè)用戶密碼update.php
html
head
titleFORM/title
/head
body
br /
h1Insert:/h1
form action="insert.php" method="post"
username:input type="name" name="username"/
br /
password:input type="password" name="password"/
input type="submit" value="submit"/
/form
br /hr /br /
h1Delete/h1
form action="delete.php" method="post"
username:input type="name" name="username" /
br /
Are you sure?input type="submit" value="sure" /
/form
br /hr /br /
h1Update/h1
form action="update.php" method="post"
username:input type="name" name="username"/
br /
You want to change your password into:input type="password" name="password"/
input type="submit" value="submit"/
/form
br /hr /br /
/body
/html
以上三個(gè)功能的提交源Operate.html
這個(gè)是要有界面的,還要有類似C#,Java這種語(yǔ)言來(lái)寫邏輯講界面和數(shù)據(jù)庫(kù)結(jié)合起來(lái),單sql是沒(méi)法實(shí)現(xiàn)這么復(fù)雜的邏輯的。
mysql存儲(chǔ)過(guò)程返回2個(gè)資源,第一個(gè)是執(zhí)行信息,第二個(gè)是存儲(chǔ)過(guò)程返回結(jié)果。
mysql_*系列函數(shù)無(wú)法獲取超過(guò)1個(gè)資源,需使用mysqli或PDO代替。
PDO:
$stmt?=?$db-prepare("CALL?pro_rb_save(?,?,@return_msg);");??
$stmt-bindParam(1,?$a);
$stmt-bindParam(2,?$b);
$stmt-execute?();
$outputArray?=?$db-query("select?@return_msg")-fetch(PDO::FETCH_ASSOC);
var_export($return_msg);
首先創(chuàng)建一個(gè)連接工廠import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;public class ConnectionFactory {
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
public ConnectionFactory() {
super();
// TODO Auto-generated constructor stub
} public void OpenConn() throws Exception{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://127.0.0.1:3306/guestbook";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
}catch(Exception e){
System.out.println("創(chuàng)建鏈接拋出異常為:"+e.getMessage());
}
} public ResultSet executeQuery(String sql) throws Exception{
try{
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}catch(Exception e){
System.out.println("執(zhí)行查詢拋出的異常為:"+e.getMessage());
}
return rs;
} public void close() throws Exception{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println("關(guān)閉對(duì)象拋出的異常:"+e.getMessage());
}
} }
測(cè)試類 import java.sql.ResultSet;public class TestJDBC {
public static void main(String[] args) {
ConnectionFactory c= new ConnectionFactory();
try {
c.OpenConn();
String sql="select * from tb_guestbook";
ResultSet rs=c.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(2));
}
c.close();
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
當(dāng)前文章:mysql怎么實(shí)現(xiàn)代碼,mysql是怎么實(shí)現(xiàn)的
文章位置:http://vcdvsql.cn/article36/hchgsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、、網(wǎng)站建設(shè)、電子商務(wù)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)