package jdbctest1;
//import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;
//import java.lang.*;
public class DbConnect {
public static void main(String[] args) {
Connection conn = getConnection("sa", "971120");
//System.out.println("Please enter the operation you want to perform:");
System.out.println("Please enter the SQL statement you want to execute:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String Sql = null;
try {
Sql = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
char[] S = Sql.toCharArray();
switch (S[0]) {
case 's':
query(conn,Sql);
break;
case 'i':
insert(conn,Sql);
break;
case 'u':
update(conn,Sql);
break;
case 'd':
delete(conn,Sql);
break;
default:
System.out.println("you entered the wrong statement");
break;
}
releaseConnection(conn);
}
//数据库连接
public static Connection getConnection(String userName, String password){
Connection conn = null;//声明连接对象
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Student";
try {
Class.forName(driver);//注册(加载)驱动程序
conn = DriverManager.getConnection(url, userName, password);//获得数据库连接
System.out.println("connect succeed!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("connect failed");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("connect failed");
}
return conn;
}
//查询数据
public static void query(Connection conn, String Sql){
//String Sql = "select * from SC";
Statement stmt;
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(Sql);//执行sql语句并返还结束
while(rs.next()){
System.out.println("sno:" + rs.getString("sno") + " cno:" + rs.getString("cno") + " grade:" + rs.getString("grade"));
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//插入数据
public static void insert(Connection conn, String Sql){
//String Sql = "insert into SC values('95002',4,89)";
try {
Statement stmt = conn.createStatement();
int count = stmt.executeUpdate(Sql);
System.out.println("you have successfully inserted " + count + " data into the table");
if(stmt != null){
try {
stmt.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//更新数据
public static void update(Connection conn,String Sql){
//String Sql = "update SC set cno = '7' where grade = '89'";
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate(Sql);
System.out.println("update succeed");
if(stmt != null){
try {
stmt.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//删除数据
public static void delete(Connection conn, String Sql){
//String Sql = "delete from SC where sno = '95004'";
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate(Sql);
System.out.println("delete succeed");
if(stmt != null){
try {
stmt.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//释放数据库连接
public static void releaseConnection(Connection conn){
if(conn != null)
try {
conn.close();
System.out.println("release succeed!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
jdbc
最新推荐文章于 2022-07-21 21:05:36 发布