import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TIJIAO {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;DatabaseName=MySchool";
String username = "sa";
String pwd = "6375196";
try {
Class.forName(driverName);
System.out.println("登录成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("登录失败");
}
try {
Connection conn = DriverManager.getConnection(url, username, pwd);
Statement stat = conn.createStatement();
String sql="insert into work values('wangdun',30,'Professional pharmacist')";
int cont = stat.executeUpdate(sql);
if (cont>0) {
System.out.println("添加成功");
}
System.out.println("数据库连接成功");
conn.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
}
注意:导包需要自己准备.
这段代码是把自己想要录入的信息上交到指定IP地址的电脑数据库中