public final static String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public final static String USER_NAME = "XXXX";
public final static String USER_PWD = "XXXXXXX";
protected Connection conn;
protected PreparedStatement pstmt;
protected ResultSet rs;
protected Connection getConn() {
Connection conn = null;
try {
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(JDBC_URL, USER_NAME, USER_PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
}
return conn;
protected void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {
if (rs != null) {
rs.close();
// TODO Auto-generated catch block
if (pstmt != null) {
pstmt.close();
if (conn != null) {
conn.close();
查:select * from XXX
增:insert into XXX value(X,X,X,X)
删:delete from XXX
改:update XXX set XXX=?,XXX=? where XXX=?
public int addNews(NewsInfo news) {
int result = 0;
conn = getConn();
pstmt = conn.prepareStatement("增加的SQL语句");
pstmt.setString(1, news.getNews_title());
result = pstmt.executeUpdate();
} finally {
closeAll(rs, pstmt, conn);
return result;
就给你这么多提示吧.
增删改查都有链接网页链接
粘贴着查询的:
package cn.web.jdbc;
import java.sql.*;
public class UserLogin {
public static void main(String[] args) {
? ? //? 加载驱动
? ? try {
? ? ? ? Class.forName("com.mysql.jdbc.Driver");
? ? ? ? // 获取连接
? ? ? ? String user = "root";
? ? ? ? //模拟前台传入的用户名和密码
? ? ? ? String InputUsername = "老八";
? ? ? ? try {
? ? ? ? ? ? //? 连接对象输入三个参数
? ? ? ? ? ? Connection connection = DriverManager.getConnection(url, user, mysqlPassword);
? ? ? ? ? ? System.out.println(connection);
? ? ? ? ? ? //定义sql语句
//? ? ? ? ? ? ? ? 查询
? ? ? ? ? ? String sql1 = "select * from student where username='" + InputUsername + "' and password='" + InputPassword + "'";
? ? ? ? ? ? System.out.println(sql1);
? ? ? ? ? ? Statement statement = connection.createStatement();
? ? ? ? ? ? ResultSet resultSet = statement.executeQuery(sql1);
? ? ? ? ? ? System.out.println(resultSet);
? ? ? ? ? ? if (resultSet.next()) {
? ? ? ? ? ? ? ? System.out.println("登录成功");
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? System.out.println("登录失败");
? ? ? ? ? ? }
//? ? ? ? ? ? ? ? 释放资源
? ? ? ? ? ? statement.close();
? ? ? ? ? ? connection.close();
resultSet.close();
? ? ? ? } catch (SQLException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? } catch (ClassNotFoundException e) {
? ? ? ? e.printStackTrace();
? ? }
①.、首先在电脑上启动数据库 ,在数据库中创建表,下面给出具体的SQL语句.
首先你得确定你的数据库连接是通过什么形式连接的,hibernate还是原生态的jdbc 还是spring;
如果是只有hibernate,那么你得通过加载配置文件得到sessionFactory,然后得到session
如果spring,那么同样也需要注入sessionfactory到你的dao
如果是jdbc方式,那么你就按照原生态jdbc写法
都说到这里了大家应该明白,在你构造DAO时,得有数据源.这样才能操纵你的数据库
如果搞懂了这些问题,那么你的第一个,第三个问题就迎刃而解了.至于第二问题,我没明白你什么意思!
原文链接随便找的还行网页链接
JDBC步骤:
加载数据库的驱动,它是java和数据库之间的桥梁
增删改代码:
public class executeUpdate {
//? 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取连接
String user = "root";
//? 连接对象输入三个参数
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
//定义sql语句
// 增
String sql1 = "insert into student(username,password) values ('诸葛亮','111111')";
// 删
// 改
Statement statement = connection.createStatement();
//? ? ? ? ? ? ? ? 修改这里的sql即可
int count = statement.executeUpdate(sql1);
System.out.println(count);
//? ?----------------------------------------------------------------
statement.close();
connection.close();
//操作acess的
package cn.zhtech;
import java.io.*;
public class DBManager {
/**
* @param args
*/
// TODO 自动生成方法存根
String strPath="";//当前程序根路径
try{
File f=new File(".");
strPath=f.getCanonicalPath();
}catch(IOException e){
System.out.println(e.toString());
//access文件路径
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+strPath+"\\data\\aa.mdb";
String username="";
String password="";
Connection con;
Statement stml;
ResultSet res;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动
con=DriverManager.getConnection(url, username, password);//获取连接
stml=con.createStatement();//建立statement
res=stml.executeQuery("select * from test");//查询
while(res.next()){//显示
System.out.println(res.getString("u_name")+"\n");
res.close();
stml.executeUpdate("insert into test(u_name) values('kkk')");//插入
stml.close();//关闭
con.close();
}catch(Exception e){
以上就是土嘎嘎小编为大家整理的java增删改查的代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!