能不能发个java链接sql的小项目给我啊 能实现增删改查的

2024-11-23 07:16:42
推荐回答(2个)
回答1:

基本的连接数据库方法,DBhelper,调用方法传sql语句就行了,数据库地址和用户密码改成你自己的,增删查改都能做,手打的,多学习多分享养成好习惯,网上很多的,这代码哪里不懂搜一搜基本都出来了,弄懂了基本就能做增删查改了

package auditUseCaces;

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

import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;

import com.google.common.hash.BloomFilter;

public class DBhelper {
public static Connection conn = null;

public static void main(String[] args) {
//插入的字段
boolean isenable = true;
Time time = null;
// 往user插用户,ID从1开始
int id = 1;
//循环插入10条数据
for (int i = 0; i <= 9; i++) {
id++;
String sql = "insert into user values(" + id + ",'app" + id
+ "','123'," + isenable + "," + time + ")";
sqlexcuteUpdate(sql, "root", "secsmarts");
}
}

/**
* 打开数据库
*
* @return
*/
public static Connection sqlconnection(String username, String password) {
// 声明一个数据库driver
String driver = "com.mysql.jdbc.Driver";
// 声明数据库链接字符串
String loclhost = "jdbc:mysql://ip:port/secsmarts?" + "user="
+ username + "&password=" + password + ""
+ "&useUnicode=true&characterEncoding=UTF-8";
// 加载驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败...");
e.printStackTrace();
}
// 链接数据库
try {
conn = DriverManager.getConnection(loclhost);
return conn;
} catch (SQLException e) {
System.out.println("链接失败...");
e.printStackTrace();
return conn;
}
}

/**
* 返回结果集
*
* @param sql
* @return resulset
*
*/
public static ResultSet sqlexcuteQuery(String sql, String username,
String password) {
try {
// 连接数据库
sqlconnection(username, password);
// 执行sql语句
PreparedStatement ps = conn.prepareStatement(sql);
// 获取返回结果集
ResultSet rows = ps.executeQuery(sql);
int count = 0;
if (rows != null) {
while (rows.next()) {
count += 1;
}
System.out.println(count);
System.out.println("成功");
}
rows.close();
ps.close();
conn.close();
return rows;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}

/**
* 返回行数
*
* @param sql
* @return int
*/
public static int sqlexcuteUpdate(String sql, String username,
String password) {
try {
sqlconnection(username, password);
PreparedStatement ps = conn.prepareStatement(sql);
int up = ps.executeUpdate();
if (up >= 0) {
System.out.println(up);
}
ps.close();
conn.close();
return up;
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}

/**
* 返回结果集
*
* @param sql
* @return boolean
*/
public static boolean sqlexcute(String sql, String username, String password) {
try {
sqlconnection(username, password);
PreparedStatement ps = conn.prepareStatement(sql);
boolean bl = ps.execute();
if (bl = true) {
System.out.println("成功!");
}
ps.close();
conn.close();
return bl;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}

/**
* 关闭Connection服务
*/
public static void ServerClose() {
try {
if (conn.isClosed() == false) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

回答2:

可以的啊,怎么给