您的位置:首页 > 编程语言 > Java开发

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

2014-12-15 15:16 225 查看
要用到的jar包:mysql-connector-java-5.1.16-bin.jar,自行到网上下载即可。

根据实际配置数据库的信息:

private String dbDriver = "com.mysql.jdbc.Driver";
private String dbUrl = "jdbc:mysql://[ip地址]:[端口号]/[数据库名]";//数据库的信息,根据实际填写
private String dbUser = "ceshi";//数据库用户名
private String dbPass = "123";  //数据库密码


首先连接数据库:
public Connection getConn()
{
Connection conn = null;
try
{
Class.forName(dbDriver).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
}
catch (ClassNotFoundException ce)
{
ce.printStackTrace();
}
catch (SQLException se)
{
se.printStackTrace();
}
catch (InstantiationException ite)
{
ite.printStackTrace();
}
catch (IllegalAccessException iae)
{
iae.printStackTrace();
}
return conn;
}


增加记录:
public int insert() {
int i = 0;
// sql语句 insert into (表名)(列名1, 列名2, 列名3, 列名4, 列名5) values(?, ?, ?, ?, ?)";
        String sql = "insert into ceshi(id, number, money, score, username) values(?, ?, ?, ?, ?)";
Connection con = getConn();
PreparedStatement preStmt = null;
try
{
preStmt = con.prepareStatement(sql);
i = preStmt.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try {
if (con != null)
con.close();
if (preStmt != null)
preStmt.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return i;//<span style="font-family: Arial; font-size: 14px; line-height: 26px;">返回影响的行数,不为0则执行成功</span>
}


修改记录:

public int update() {
int i = 0;
//sql语句 update (表名) set (列名1) = ?, , (列名2) = ?, (列名3) = ?, (列名4) = ? where (列名1)=?
//提示:注意要有where条件,具体如下
String sql = "update ceshi set number =? , money = ? , score = ? , username = ? where id= ?";
Connection con = getConn();
PreparedStatement preStmt = null;

try
{
preStmt = con.prepareStatement(sql);
i = preStmt.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally  //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">使用完就得关闭流,释放资源</span>
{
try {
if (con != null)
con.close();
if (preStmt != null)
preStmt.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return i;//<span style="font-family: Arial; font-size: 14px; line-height: 26px;">返回影响的行数,1为执行成功</span>
}


查询记录:
public String select() {
String sql = "select * from Bt_Users";
Connection con = getConn();
Statement stmt = null;
ResultSet rs = null;

try
{
stmt = con.createStatement();
rs = stmt.executeQuery(sql);

while (rs.next())
{
String name1 = rs.getString("UserID");
String name2 = rs.getString("AreaCode");
String name3 = rs.getString("MobileNumber");
String name4 = rs.getString("UserAccounts");
String name5 = rs.getString("UserPasswords");
String name6 = rs.getString("UserPhoto");
String name7 = rs.getString("UserEmail");
String name8 = rs.getString("UserQQNumber");
String name9 = rs.getString("RegistrationDate");
String name10 = rs.getString("Manufacturer");
String name11 = rs.getString("DeviceModel");
String name12 = rs.getString("DeviceIMEI");
String name13 = rs.getString("DeviceNumber");
String name14 = rs.getString("OperatingSystem");
String name15 = rs.getString("SystemVersion");
String name16 = rs.getString("ScreenResolution");
String name17 = rs.getString("Provinces");
String name18 = rs.getString("CityName");
String name19 = rs.getString("LastOnLine");

String name=name1+" "+name2+" "+name3+" "+name4+name5+" "+name6+" "+name7+" "+name8+" "
+name9+" "+name10+" "+name11+" "+name12+" "+name13+" "+name14+" "+name15+" "+name16+" "+name17+" "+name18+" "+name19;
System.out.println(name);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if (con != null)
con.close();
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return "success";//<span style="font-family: Arial; font-size: 14px; line-height: 26px;">具体返回根据个人业务而定</span>
}


删除记录:

public int delete() {
String sql = "delete from Bt_Users where UserID =123456 ";
int i = 0;
Connection con = getConn();
Statement stmt = null;

try
{
stmt = con.createStatement();
i = stmt.executeUpdate(sql);
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try{
if(con != null)
con.close();
if(stmt != null)
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return i;//<span style="font-family: Arial; font-size: 14px; line-height: 26px;">如果返回的是1,则执行成功;</span>
}


测试函数:

public static void main(String[] args) {
// 测试
ceshi cs = new ceshi();
//cs.insert();// 添加
//cs.update();// 修改
cs.select();// 查询
//cs.delete();// 删除
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: