「java数据库的操作语句」java数据库的操作语句包括
本篇文章给大家谈谈java数据库的操作语句,以及java数据库的操作语句包括对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java怎么操作数据库呢?
- 2、在Java中如何对数据库中的数据进行操作?
- 3、java连接数据库的代码
- 4、怎么使用JAVA连接数据库?
- 5、java的数据库操作
- 6、如何在java中实现数据库语句的调用
java怎么操作数据库呢?
jdbc连接数据库,DriverManger等初始化连接,prepareStatement语句执行sql,ResultSet拿到执行结果,while result.hasNext循环拿到数据值等。
如果不用jdbc,可以用框架,hibernate,mybatis都是可以的。
详情可以私聊我,手机打字太累了。
在Java中如何对数据库中的数据进行操作?
package com.dao;import java.sql.*;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;public class BaseDao {
/**
* 创建数据库连接及关闭
*/
// 打开连接
public static Connection getConnection() {
Connection con = null; /*************************** oracl 的连接 ***************************************/
// try { // Class.forName("oracle.jdbc.driver.OracleDriver");
// con = DriverManager.getConnection(
// "jdbc:oracle:thin:@127.0.0.1:1521:orcl", "bbs", "sa");
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// } catch (SQLException e) {
// e.printStackTrace();
// }
/******************************* sqlerver 的连接 ******************************/
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;databasename=bbs", "sa",
"zhou");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
/*********************************************************************/
return con;
} // 关闭
public static void closeAll(Connection connection,
PreparedStatement pStatement, ResultSet res) {
try {
if (connection != null (!connection.isClosed())) {
connection.close();
}
if (res != null) {
res.close();
res = null;
}
if (pStatement != null) {
pStatement.close();
pStatement = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
对数据库增删改查package com.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import com.entity.News;public class NewsDao {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
/**
* 添加新闻
* @param news
* @return
*/
public boolean newsAdd(News news){
boolean result=false;
String sql="insert into news values(?,?)";
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement(sql);
pstmt.setString(1, news.getContent());
pstmt.setString(2, FormatTime.newTime());
int i = 0;
i = pstmt.executeUpdate();
if (i 0)
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 修改新闻
* @param news
* @return
*/
public boolean updateNews(News news){
boolean result=false;
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement("update news set content=? ,writedate=? where newsid=?");
pstmt.setString(1, news.getContent());
pstmt.setString(2, FormatTime.newTime());
pstmt.setInt(3, news.getNewsID());
int i = 0;
i = pstmt.executeUpdate();
if (i 0)
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 删除新闻
* @param news
* @return
*/
public boolean deleteNews(News news){
boolean result=false;
String sql=String.format("delete from news where newsid=%d", news.getNewsID());
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement(sql);
int i = 0;
i = pstmt.executeUpdate();
if (i 0)
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 删除新闻 重载
* @param newsId
* @return
*/
public boolean deleteNews(int newsId){
boolean result=false;
String sql=String.format("delete from news where newsid=%d", newsId);
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement(sql);
int i = 0;
i = pstmt.executeUpdate();
if (i 0)
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 查询所有的新闻
* @return
*/
public ListNews selectAllNews(){
ListNews list=new ArrayListNews();
String sql="select * from Users";
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
News news=new News();
news.setNewsID(rs.getInt(1));
news.setContent(rs.getString(2));
news.setWriteDate(rs.getString(3));
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
BaseDao.closeAll(rs, pstmt, con);
}
return list;
}
/**
* 查询单个
* @return
*/
public News selectOneNews(){
News news=new News();
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement("select top 1 * from news order by newsid desc");
rs=pstmt.executeQuery();
while(rs.next()){
news.setNewsID(rs.getInt(1));
news.setContent(rs.getString(2));
news.setWriteDate(rs.getString(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
BaseDao.closeAll(rs, pstmt, con);
}
return news;
}
}
实体类package com.entity;import java.io.Serializable;
public class News implements Serializable{
private int newsID;
private String content;
private String writeDate; public News() {
super();
// TODO Auto-generated constructor stub
} public News(String content, String writeDate) {
super();
this.content = content;
this.writeDate = writeDate;
} public News(int newsID, String content, String writeDate) {
super();
this.newsID = newsID;
this.content = content;
this.writeDate = writeDate;
} public int getNewsID() {
return newsID;
} public void setNewsID(int newsID) {
this.newsID = newsID;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public String getWriteDate() {
return writeDate;
} public void setWriteDate(String writeDate) {
this.writeDate = writeDate;
}
}
java连接数据库的代码
package mysql;
import java.sql.*;
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("数据库连接成功");
} else {
System.out.println("数据库连接失败");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 实例化 Statement 对象
Statement statement = getConnection().createStatement();
// 要执行的 Mysql 数据库操作语句(增、删、改、查)
String sql = "";
// 展开结果集数据库
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通过字段检索
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 输出数据
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
// 完成后需要依次关闭
resultSet.close();
statement.close();
getConnection().close();
}
}
怎么使用JAVA连接数据库?
1、加载驱动程序。
2、创建连接对象。
3、创建sql语句执行对象 。
4、执行sql语句。
5、对执行结果进行处理。
6、关闭相关的连接对象即可(顺序跟声明的顺序相反)。
处理结果两种情况:
1、执行更新返回的是本次操作影响到的记录数。
2、执行查询返回的结果是一个ResultSet对象。
ResultSet包含符合SQL语句中条件的所有行,并且它通过一套get方法提供了对这些 行中数据的访问。
扩展资料:
Statement
要执行SQL语句,必须获得java.sql.Statement实例,Statement实例分为以下3 种类型:
1、执行静态SQL语句。通常通过Statement实例实现。
2、执行动态SQL语句。通常通过PreparedStatement实例实现。
3、执行数据库存储过程。通常通过CallableStatement实例实现。
参考资料:百度百科JAVA
java的数据库操作
ResultSet 是返回值
比如你要用select * from 表名 来查询这张表的数据
那么你查询出来的数据总得返回给你 好叫你看见你查询出来的是什么.
那么这个返回会来的数据就在ResultSet里
通过遍历ResultSet 就可以获取你查询出来的数据了
至于怎么遍历ResultSet 还请楼主请教老师或者baidu/google.......
如何在java中实现数据库语句的调用
不清楚你想连接什么数据库 以下是一些数据库的连接:
mySQl:
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3307/数据库名";
String user="root";
String password="";
conn= DriverManager.getConnection(url,user,password);
st=conn.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}
SQLSERVER:
Connection conn=null;
Statement st=null;
ResultSet rs=null;
PreparedStatement upst = null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=数据库名";
String user="sa";
String password="";
conn= DriverManager.getConnection(url,user,password);
}
catch(Exception e)
{
e.printStackTrace();
}
ORACLE:
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); // 加载驱动类
// 注册JDBC驱动
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
String sourceURL = "jdbc:oracle:thin:@localhost::1521:orcl"; //localhost视具体情况需要改变
String user = "scott";//用户
String password = "tiger";//密码
con = DriverManager.getConnection(sourceURL,user,password); // 建立连接
}catch (Exception e){
e.printStackTrace();
}
执行SQL:类似的
stmt = conn.createStatement();
String sql = "insert into ……";// sql
stmt.executeUpdate(sql);
java数据库的操作语句的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java数据库的操作语句包括、java数据库的操作语句的信息别忘了在本站进行查找喔。
发布于:2022-12-06,除非注明,否则均为
原创文章,转载请注明出处。