导航:首页 > 编程语言 > 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语句相关的资料

热点内容
战锤40kpdf 浏览:375
java判断字符串是汉字 浏览:462
2017初级教材pdf 浏览:451
松下空调压缩机品牌 浏览:817
python复选框 浏览:746
反诈中心app是什么时候出来的 浏览:586
魔兽世界需要什么服务器地址 浏览:189
啥是单片机休眠 浏览:104
什么音乐app最好 浏览:927
牙膏自制解压神器 浏览:327
卸载linuxwps 浏览:226
threadjavarunnable 浏览:358
一元线性回归计算法 浏览:949
刷宝app是什么鬼 浏览:744
linux动态加载模块 浏览:25
javamd5工具类 浏览:76
安保嘉奖命令 浏览:647
bt种子能压缩吗 浏览:676
JAVA特点是什么 浏览:886
字幕网app怎么找 浏览:533