Java连接MySQL数据库实现增删改查操作!

package dbConnPackage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnToMySql {

/**
*
@param args
*/

public static void main(String[] args) {

// TODO Auto-generated method stub
try {

Class.forName("com.mysql.jdbc.Driver");
//System.out.println("Loading mySql-connector-java.jar successed!");
} catch (ClassNotFoundException e1) {

//System.out.println("Cann't find mySql-connector-java.jar!");
e1.printStackTrace();

}

String url = "jdbc:mysql://localhost:3306/student";
Connection conn;

try {
conn = DriverManager.getConnection(url, "root", "3312");
Statement stmt = conn.createStatement();
//System.out.println("mySqlDB Connected!");

String updateSql = "update studentInfo set Telephone = ? , StuName = ? where StuID = ?";
PreparedStatement pst = conn.prepareStatement(updateSql);
pst.setString(1, "133556565");
pst.setString(2, "Alice");
pst.setString(3, "20120002");
pst.executeUpdate();

String delSql = "delete from studentInfo where StuID = ?";
pst = conn.prepareStatement(delSql);
pst.setString(1, "20120001");
pst.executeUpdate();

String insertSql = "insert into studentInfo values(?, ?, ?)";
pst = conn.prepareStatement(insertSql);
pst.setString(1, "20120004");
pst.setString(2,"Bob");
pst.setString(3,"189111");
pst.executeUpdate();

String selectSql = "select * from studentInfo";
ResultSet rs = stmt.executeQuery(selectSql);
System.out.println("StuID\t" + "StuName\t" + "Telephone");
while (rs.next()) {
System.out.print(rs.getString(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.println(rs.getString(3));
}

rs.close();
stmt.close();
conn.close();

} catch (SQLException e) {
e.printStackTrace();
}

}

}

数据库操作 Java视频大讲堂 视频教程

http://school.itzcn.com/video-vid-2308-spid-50.html

http://school.itzcn.com/video-vid-2309-spid-50.html

http://school.itzcn.com/video-vid-2310-spid-50.html

转载于:https://www.cnblogs.com/abbi7712/archive/2012/03/05/2381088.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值