重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
首先你要在项目中加载对应数据库的驱动包,然后进行数据库查询,代码待会给你。
成都创新互联主营广南网站建设的网络公司,主营网站建设方案,成都APP应用开发,广南h5微信小程序开发搭建,广南网站营销推广欢迎广南等地区企业咨询
String sql="";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc.mysql//localhost:3306/你的数据库名","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);
?
从服务器中获取用户所有信息(SQL SELECT语句)并以表格形式出现
?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);
?
注册一个新用户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);
?
修改一个用户密码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
以上三个功能的提交源Operate.html
这个是要有界面的,还要有类似C#,Java这种语言来写逻辑讲界面和数据库结合起来,单sql是没法实现这么复杂的逻辑的。
mysql存储过程返回2个资源,第一个是执行信息,第二个是存储过程返回结果。
mysql_*系列函数无法获取超过1个资源,需使用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);
首先创建一个连接工厂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("创建链接抛出异常为:"+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("执行查询抛出的异常为:"+e.getMessage());
}
return rs;
} public void close() throws Exception{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println("关闭对象抛出的异常:"+e.getMessage());
}
} }
测试类 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();
}
}
}