求java进销存代码要与MySql数据库连接,哪位朋友有,请帮帮忙!!!

进销存管理系统要求与MySQL数据库连接!!!... 进销存管理系统 要求与MySQL数据库连接!!! 展开
 我来答
极度放肆
2009-12-22 · TA获得超过135个赞
知道答主
回答量:363
采纳率:0%
帮助的人:116万
展开全部
我封装的DB类

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 mySQLDB {
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myBBS", "root" , "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}

public static Statement createStmt(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}

public static ResultSet executeQuery(Statement stmt ,String sql){
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}

public static PreparedStatement prepareStmt(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}

public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}

public static void close(Statement stmt) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}

public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}
总是啥都不懂
2009-12-22 · 超过39用户采纳过TA的回答
知道答主
回答量:67
采纳率:0%
帮助的人:0
展开全部
Class.forName("com.mysql.jdbc.Driver");

connsql = "jdbc:mysql://"+【yourhost】"+【yourdatabase】+"?user="+【yourusername】+"&password="+【yourpass】+"&" +
"useUnicode=true&characterEncoding=UTF-8";

Connection conn = DriverManager.getConnection(connsql);
Statement stat = conn.createStatement();
//存
String insert_sql = "insert into yourtable(key1,key2,key4) value(value1,value2,value4)";

stat.executeUpdate(insert_sql);
//取
String select_sql = "select * from yourtable where id = 1";
ResultSet rs = stat.executeQuery(select_sql);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
}
//删
String del_sql = "delete form yourtable where id = 1";
stat.executeUpdate(del_sql);
//改
String update_sql = "update yourtable set name = "+"\'"+newname+"\'"+" where id = 1";
stat.executeUpdate(update_sql);

if(rs != null)rs.close();
if(stat != null)stat.close();
if(conn != null)conn.close();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式