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{//define Connection Driver Class.forName (dbdriver); } catch (Java.lang.ClassNotFoundException E) {System.err.println ("DBconnection ():" +e.getmessage ()); }//--------connect the SQL database------------------try {conn = drivermanager.getconnection (Strconnection,user,password); } catch (SQLException ex) {System.err.println ("Aq.executequery:" +ex.getmessage ()); } return conn; }
}
Use Jtds to connect to the SQL Server database and get all database names