package testMain;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestMain {
catch(java.lang.ClassNotFoundException e){ System.out.println("hello world"); Connection conn = getConnection(); try { Statement st=conn.createStatement(); //st.execute("insert into dept(name) values(\‘5555\‘)"); ResultSet rset=st.executeQuery("SELECT Name from Master..SysDatabases ORDER BY Name" ); int rowCount = 0; while(rset.next()) { System.out.println(rset.getString("name")); rowCount++; } System.out.println(rowCount); st.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection(){ String dbDriver = "net.sourceforge.jtds.jdbc.Driver"; String strConnection = "jdbc:jtds:sqlserver://localhost:1433/"; String user = "sa"; String password = "123"; Connection conn = null; try{ //定义连接驱动 Class.forName(dbDriver); } catch(java.lang.ClassNotFoundException e){ System.err.println("DBconnection():"+e.getMessage()); } //--------连接SQL数据库------------------ try { conn = DriverManager.getConnection(strConnection,user,password); } catch(SQLException ex) { System.err.println("aq.executeQuery:"+ex.getMessage()); } return conn; }
}
时间: 2024-11-13 10:03:29