java jdbcTemplate操作: 我需要在一个表中(两个字段,id和description)查询数据,表中有没有数据是

2025-02-22 01:55:34
推荐回答(2个)
回答1:

package hu;
import java.sql.*;
public class Sql_jdbc {
public static void fw(String num) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 ,我用的是SQL server数据库
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=xxx"; //连接服务器和数据库test,xxx表示你的建的数据库名
String userName = "hu"; //默认用户名,我的,你的你自己知道吗
String userPwd = "123"; //密码,我的,你设置的你自己知道的
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName(driverName);
con= DriverManager.getConnection(dbURL,userName,userPwd);
stmt=con.createStatement();
rs=stmt.executeQuery("select * from xxx where id='+yy.tostring()+"");//xx表示表,yy表示将要插入的这条记录的id,一般你都会定义一个变量的,或者是一个 个文本框的内容
if(rs.next()==true){

System.out.print("这个记录已经存在");//表示这个记录存在,看看if的条件怎么写的,下次记住了啊

}
else{
//这里可以写插入语句了,具体我就不写了,如果你不会,我只能说呵呵
}
stmt.close();
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
fw("1");
}
}

回答2:

应该先调用query 再调用update方法呀