Difference between revisions of "Example java code here"
From MyWiki
(Created page with "<source lang="java"> mport java.sql.*; class MysqlCon{ public static void main(String args[]){ try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | Sample code which connects to sql server 2012 database on ip address of 192.168.56.111<br> | ||
+ | |||
+ | |||
<source lang="java"> | <source lang="java"> | ||
− | + | import java.sql.*; | |
class MysqlCon{ | class MysqlCon{ | ||
Line 6: | Line 9: | ||
try{ | try{ | ||
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); | Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); | ||
− | Connection con=DriverManager.getConnection("jdbc:sqlserver://192.168.56.111:1433;databaseName=georges_database"," | + | Connection con=DriverManager.getConnection("jdbc:sqlserver://192.168.56.111:1433;databaseName=georges_database","*****","******"); |
Statement stmt=con.createStatement(); | Statement stmt=con.createStatement(); | ||
ResultSet rs=stmt.executeQuery("select * from mytable"); | ResultSet rs=stmt.executeQuery("select * from mytable"); |
Latest revision as of 11:47, 5 February 2018
Sample code which connects to sql server 2012 database on ip address of 192.168.56.111
import java.sql.*; class MysqlCon{ public static void main(String args[]){ try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con=DriverManager.getConnection("jdbc:sqlserver://192.168.56.111:1433;databaseName=georges_database","*****","******"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from mytable"); while(rs.next()) System.out.println(rs.getString(1)+" "+rs.getString(2) ); con.close(); }catch(Exception e){ System.out.println(e);} } }