添加
package tainjia;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Add {
/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://Localhost:1433;DatabaseName=S2226";
String usernaem="sa";
String pwd="444555";
Class.forName(driver);
Connection con=DriverManager.getConnection(url,usernaem,pwd);
Statement sta=con.createStatement();
String sql="insert into Student values('李四',2000)";
String name="select * from Student";
ResultSet res=sta.executeQuery(name);
if(res!=null){
while(res.next())
{
System.out.println(res.getInt("Sid"));
}
}
int count=sta.executeUpdate(sql);
if(count>0)
{
System.out.println("添加数据成功");
}
sta.close();
con.close();
}
}
删除
package delete;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class delect {
/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://Localhost:1433;DatabaseName=S2226";
String username="sa";
String pwd="444555";
Connection con;
Class.forName(driver);
con=DriverManager.getConnection(url,username,pwd);
String sql="delete from Student where Sid=4";
PreparedStatement ps=con.prepareStatement(sql);
int count=ps.executeUpdate();
if(ps!=null)
{
if(count>0)
{
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
/*
if(count>0)
{
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}*/
}
}
登录package denglu;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
public class denglu {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
Connection con=null;
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url="jdbc:sqlserver://Localhost:1433;DatabaseName=S2226";
String username="sa";
String pwd="444555";
try {
con=DriverManager.getConnection(url,username,pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner input=new Scanner(System.in);
System.out.println("请输入用户名");
String name=input.nextLine();
System.out.println("请输入密码");
String pwd1=input.nextLine();
String sql="select count(1) as num from Student where Sname=? and Scalary=?";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,pwd1);
ResultSet res=ps.executeQuery();
if(res!=null){
while(res.next())
{
//注释快捷键 ctrl+shift+/
int count=res.getInt("num");
if(count>0){
System.out.println("登陆成功");
}else{
System.out.println("登录失败");
}
}
}
}
}
baseDao
package bao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
public class baseDAO {
public static final String DIVER_STRING="com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String URL_STRING="jdbc:sqlserver://192.168.60.124:1433;DatabaseName=Subway";
public static final String USERNAME_STRING="sa";
public static final String PWD_STRING="6375196";
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
public int executeUpdate(String sql,Object...objects)throws SQLException
{ ps=con.prepareStatement(sql);
for (int i = 0; i <objects.length; i++) {
ps.setObject(i+1, 1);
}
int count=ps.executeUpdate();
return count;
}
//读取操作
public ResultSet executeQuery(String sql,Object...objs)throws SQLException
{ps=con.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
ps.setObject(i+1, objs[i]);
}
rs=ps.executeQuery();
return rs;
}
static{
try {
Class.forName(DIVER_STRING);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnection()throws SQLException
{
if(con==null||con.isClosed()){
con=DriverManager.getConnection(URL_STRING,USERNAME_STRING,PWD_STRING);
}
return con;
}
public void closeResources() {
// TODO Auto-generated method stub
}
}
Testpackage bao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
/* // TODO Auto-generated method stub
String url="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String username="sa";
String pwd="6375196";
Connection con=DriverManager.getConnection(url, username, pwd);
String sql="select * from subwayinfo id=?";
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1, 1);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
String subwayname=rs.getString("subwayname");
System.out.println(subwayname);
}
rs.close();
ps.close();
con.close();*/
baseDAO dao=new baseDAO();
String sql="select * from subwayinfo where id=?";
Object [] objs={1};
dao.getConnection();
ResultSet rs=dao.executeQuery(sql, objs);
if(rs.next())
{
System.out.println(rs.getString("subwayname"));
}
dao.closeResources();
}
}