导航:首页 > 编程语言 > java写sql语句

java写sql语句

发布时间:2023-06-02 07:48:27

java如何执行sql语句

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;


public class xxxx {

public static void main(String[] args) {

Connection con = null ;

Statement stmt = null ;

try {

Class.forName("com.mysql.jdbc.Driver"); //mysql为例 不一样的数据库所需的驱动包不一样 连接语句略有不同

con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/数据库名", "root", "密码");

stmt = con.createStatement();

String sql = "insert into info values ('用户', 'mima', 'piapiapia~')";

stmt.executeUpdate(sql);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if(stmt != null) {

stmt.close();

stmt = null;

}

if (con != null) {

con.close();

con = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}


}


② java模糊查询中的sql语句怎么写

这语句是查询任意位置出现的字符串了

String sql="select * from employeeform where ename like '%'"+fname+"'%'";

什么数据库呢。。。

③ java 中如何使用sql插入语句

public int save(Notices notice) throws SQLException{
String sql = "insert into noticeinfo(notice_title,notice_type,notice_content,notice_add_time,user_id,user_table_id,class_table_id,notice_state,is_important)"+"values(?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = super.getConnection().prepareStatement(sql);
pstmt.setString(1, notice.getNotice_title());
pstmt.setInt(2, notice.getNotice_type());
pstmt.setString(3, notice.getNotice_content());
pstmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
pstmt.setString(5, notice.getUser_id());
pstmt.setInt(6, notice.getUser_table_id());
pstmt.setInt(7, notice.getClass_table_id());
pstmt.setInt(8, notice.getNotice_state());
pstmt.setInt(9, notice.getIs_important());
int i = pstmt.executeUpdate();
return i;
}

④ 在java中写一段很长的SQL语句该怎么写 我这个该怎么改

说个简单点的办法,你这个SQL语句别分行,比如你的sql语句是这样的。
select "+ uid +"from " +t_user_log +" where name = "+ name+ “and date between '”+
date +"'"

写成类似于这种的。
你这SQL语句我看着没什么问题配模,你扔到你数据库里看看能不能查出来。我拼SQL语句一直都是先写好SQL语句培神缓,然后把条件换成“”.双引号和括号再做处理,你这么走几遍就能看出是瞎塌哪有问题了。

⑤ java中执行sql插入语句怎么弄

1、Connection conn = DriverManager.getConnection(URL,数据库登录名,数据库登录密码);//获得数据库连接。


2、Statement statement = con.createStatement(); //访问数据库。


3、ResultSet resultSet = statement.executeQuery(sql);//执行SQL语句。

阅读全文

与java写sql语句相关的资料

热点内容
胸7椎体轻度压缩 浏览:108
sk5服务器什么意思 浏览:554
什么是廊坊交警app 浏览:294
衣柜造价算法 浏览:984
默认的web服务器地址 浏览:694
单片机与发光二极管 浏览:320
pythonwebmodule 浏览:328
空调压缩机不停了 浏览:115
python序列怎么取 浏览:199
线上数据库加密怎么查询 浏览:794
js中数据加密 浏览:470
穴pdf 浏览:549
阿里云服务器云数据库还需要吗 浏览:146
在程序设计中常用的算法有哪些 浏览:977
为什么苏州公积金app一直维护 浏览:805
有ip地址但是dhcp服务器 浏览:446
三星手机加密中断怎么回事 浏览:538
训练模型init源码 浏览:840
程序编译是谁的功能 浏览:505
qq收藏怎样设置加密 浏览:291