package homework;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DEMO {
private static final String Select = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;DatabaseName = STUDENT";
Connection con =null;
PreparedStatement statement = null;
ResultSet res = null;
try {
Class.forName(driverName);
con = DriverManager.getConnection(url,"sa","325070");
String sql = "select *from student";
statement = con.prepareStatement(sql);
res = statement.executeQuery();
while(res.next())
{
String title = res.getString("Sno");
String id = res.getString(1);
System.out.println(id+"学号:"+title);
}
}
catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(res != null) res.close();
if(statement != null) statement.close();
if(con != null) con.close();
}catch(Exception e2) {
e2.printStackTrace();
}
}
}
}
Java与sql 进行连接,部分代码JDBC
最新推荐文章于 2022-09-04 21:47:07 发布