eclipse(jdbc)与sqlserver链接 实现最基本的插入删除修改查询

注释起来的依次打开即可

package pkg;
import java.sql.*;

public class Main {
public static void main(String [] args)
{
String driverName=“com.microsoft.sqlserver.jdbc.SQLServerDriver”;
String dbURL=“jdbc:sqlserver://localhost:1433;DatabaseName=你的数据库名字”;
String userName=“你的sqlserver用户名”;
String userPwd=“你的登录密码”;

try
{
Class.forName(driverName);
Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println(“连接数据库成功”);
Statement stat=dbConn.createStatement();
/* 一、查询
* ResultSet rs=stat.executeQuery("select * from Department ");
while(rs.next()) {
String cno=rs.getString(1);
String cname=rs.getString(2);
String tno=rs.getString(3);
String t=rs.getString(4);
System.out.println("DepartmentID “+cno+” DepartmentName “+cname +” LeaderName "+tno+"Office "+t);
}/
/二、添加数据
int i= stat.executeUpdate(“insert into Department(DepartmentID,DepartmentName,LeaderName,Office) values(‘9’,‘信息工程学院’,‘许铭’,‘公教三’)”);
System.out.println(“成功添加”+i+“行”);
/
/
三、修改
int i= stat.executeUpdate(“update Department set LeaderName=‘孙靖’ where DepartmentID=‘9’”);
System.out.println(“成功修改”+i+“行”);/
/
//四、删除
int i= stat.executeUpdate(“delete from Department where DepartmentID=‘9’”);
System.out.println(“成功删除”+i+“行”);*/
stat.close();
dbConn.close();
}
catch(Exception e)
{
e.printStackTrace();
System.out.print(“连接失败”);
}
}
}

おすすめ

転載: blog.csdn.net/cherry12100511/article/details/104791925