JDBC连接SQL2005怎么进行 修改 删除 增加

JDBC连接SQL2005怎么进行 修改 删除 增加,第1张

Statement

st=con.createStatement()

ResultSet

rs=st.executeQuery("select

*

from

table")//查询 *** 作

while(rs.next()){

System.out.println(rs.getString(1))

}

st.execute("sql语句")//这个可以是增加、删除、修改

sql语句写正确就可以了

你会数据库链接,难道不会 *** 作?不可思意,你学的时候只学到链接?上面是比较简单的介绍,具体的你可以再网上找到很多相关的例子

在使用jdbc将mysql中的数据查询出来更新到sqlserve数据库时,怎么自动转换类型

JDBC *** 作MySQL数据库的步骤

1、准备MySQL数据库驱动包:mysql-connector-java-5.0.8-bin.jar,一个项目中只能存在一个版本的驱动包

a、复制该驱动包,粘贴到项目中

b、选中项目里的驱动包,右键->Build Path->Add to Build Path

2、在类里写代码加载驱:决定连接哪种数据库

a、Class.forName("com.mysql.jdbc.Driver")

b、必须进行异常处理:ClassNotFoundException

3、连接数据库

a、Connection con=DriverManager.getConnection("连接字符串", "用户名", "密码")

b、连接字符串格式固定,不同数据库,格式不同:jdbc:mysql://要连接的计算机名称:端口号/要连接的数据库名称

c、必须进行异常处理:SQLException

4、拼写要执行的sql语句,必须是可以在数据库中执行的

5、创建执行sql语句的对象

a、Statement stmt=con.createStatement()

b、注意:Statement必须来自于java.sql包中

6、执行sql语句

a、执行insert、update和delete语句:int row=stmt.executeUpdate(sql)返回影响行数

b、执行查询语句:ResultSet rs=stmt.executeQuery(sql)返回查询结果

c、执行任意sql语句(DDL、DCL、DML和DQL等)

7、对执行结果进行处理

a、执行更新语句:判断影响行数是否为0,0表示失败,非0表示成功

b、对查询结果进行处理:

1) 结果集需要先移动、后取值 :rs.next()int id=rs.getInt(1)

String name=rs.getString("loginName")

2) 结果集有多条时,需要循环 *** 作:

while(rs.next()){System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4)+"\t"+rs.getInt(5))

}

3) 不确定是否有查询结果时:if(rs.next()){说明有查询结果}else{没有查询结果}

4) 使用了聚合函数,一定有查询结果,查询结果是一行一列:

rs.next()

int result=rs.getInt(1)

注意:结果集取值时取出的时查询语句中包含的字段,与表中字段无关

9、关闭相关对象(先关闭结果集对象、在关闭执行语句对象,最后关闭连接对象)

例如:执行查询语句

Scanner input=new Scanner(System.in)

System.out.print("请输入登录名: ")

String name=input.next()

System.out.print("请输入密码: ")

String pass=input.next()

try {

Class.forName("com.mysql.jdbc.Driver")

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "root")

String sql="select COUNT(*) from UserInfo where loginName='"+name+"' and loginPass='"+pass+"'"

Statement stmt=con.createStatement()

ResultSet rs=stmt.executeQuery(sql)

rs.next()

int result=rs.getInt(1)

if(result!=0){

System.out.println("登录成功!")

}else{

System.out.println("用户名或密码错误,请重新登录!")

}

rs.close()

stmt.close()

con.close()

} catch (ClassNotFoundException e) {

System.out.println("加载驱动错误:"+e.getMessage())

} catch (SQLException e) {

System.out.println("数据库 *** 作错误:"+e.getMessage())

}

执行添加、修改和删除语句

try {

//加载驱动

Class.forName("com.mysql.jdbc.Driver")

//连接数据库

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "root")

//拼写要执行的sql语句

String sql="update UserInfo set loginPass='111' where loginName='a'"

//String sql="insert UserInfo values(default,'test','test')"

//String sql="delete from UserInfo where loginName='a'"

//创建执行语句对象

Statement stmt=con.createStatement()

//执行

int row=stmt.executeUpdate(sql)

//处理结果

if(row==0){

System.out.println("修改失败!")

}else{

System.out.println("修改成功!")

}

//关闭

stmt.close()

con.close()

} catch (ClassNotFoundException e) {

System.out.println("驱动加载错误:"+e.getMessage())

} catch (SQLException e) {

System.out.println("数据库 *** 作错误:"+e.getMessage())

}

import java.sql.*

public class JDBC {

public static void main(String[] args) {

//数据源的连接

Connection conn

driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"//驱动的名称

String url = "jdbc:odbc:xiao"//连接到一个数据源

try {

Class.forName(driverName)

conn = DriverManager.getConnection(url)//建立连接

String sql = "select * from qwe"

Statement st = conn.createStatement()

ResultSet rst = st.executeQuery(sql)//执行查询语句

while (rst.next()) {

int ID = rst.getInt(1)//获取第一行内容

String name = rst.getString(2)//第二行

System.out.println(ID +" "+ name)//输出所有记录

}

st.close()

conn.close()

} catch (ClassNotFoundException e) {

System.out.println(e)

} catch (SQLException e) {

System.out.println(e.getMessage())

}

}

}

上面的代码是查询一个指向xiao数据源,数据库名字为long的一段代码里一个表格内容的。(当然,数据库里本来得有记录)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10712711.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-10
下一篇2023-05-10

发表评论

登录后才能评论

评论列表(0条)

    保存