package lianxi2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTool {
/**
* 做连接的操作
*/
public static Statement getConnection(String path){
Statement statement = null;
// 1 添加驱动
try {
Class.forName("org.sqlite.JDBC");
//2 建立连接
Connection conn = DriverManager.getConnection("jdbc:sqlite:/"+path);
// 磁盘用小写
// 3 添加执行sql语句的对象
statement = conn.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return statement;
}
}
package lianxi2;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlUtil {
/**
* 查
*/
/**
*
* @param table
* 表名
* @param whereCause
* 传递where中条件列名
* @param where
* 条件中跟列名对应的值
*/
public static void query(String table, String whereCause, String where,
Statement statement) {
String sql = "select * from " + table + " where " + whereCause + "="
+ where;
try {
ResultSet set = statement.executeQuery(sql);
// 循环打印结果
while (set.next()) {
System.out.println(set.getString("_id") + "|"
+ set.getString(2) + "|" + set.getString(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void query(String sql, Statement statement) {
try {
ResultSet set = statement.executeQuery(sql);
// 循环打印结果
while (set.next()) {
System.out.println(set.getString("_id") + "|"
+ set.getString(2) + "|" + set.getString(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*增
*insert
* @throws SQLException
*/
public static int insert(String sql,Statement statement) throws SQLException{
return statement.executeUpdate(sql);
}
/**
* 删
* @throws SQLException
*/
public static int delete(String sql,Statement statement) throws SQLException{
return statement.executeUpdate(sql);
}
/**
* 改
* @throws SQLException
*/
public static int update(String sql,Statement statement) throws SQLException{
return statement.executeUpdate(sql);
}
}
package lianxi2;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException, UnsupportedEncodingException {
Statement statement = JDBCTool.getConnection("d:/demo.db;");
// 插入数据
// sql语句
String sql2 = "insert into users(name,age) values('上方宝剑',1000)";
//
// //如果其为更新计数或者不存在任何结果,则返回 false
// boolean b = statement.execute(sql2);
// if(b){
// System.out.println("插入成功");
// }else {
// System.out.println("真失败");
// }
System.out.println("插入成功条数"+SqlUtil.insert(sql2, statement));
// 删除操作
String sql3 = "delete from users where name='上方宝剑'";
// int a = statement.executeUpdate(sql3);
System.out.println("删除的条数"+SqlUtil.delete(sql3, statement));
// 修改
String sql4 = "update users set name = '李四' where _id = 3";
System.out.println("修改的条数"+SqlUtil.update(sql4, statement));
// 查询
SqlUtil.query("select * from users", statement);
}
}