1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tase.db;
import java.sql.*;
import java.util.*;
import tase.data.Henkilo;
public class HenkiloDbCon {
private Connection con;
private Statement stmt;
private static String jdbcdriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url = "jdbc:sqlserver://localhost;databaseName=Henkilot;selectMethod=cursor";
public HenkiloDbCon(String tunnus, String salasana)
{
try
{
//Initialize and load the JDBC driver.
Class.forName(jdbcdriver);
getJdbcDbConnection(tunnus, salasana);
} catch (ClassNotFoundException e)
{
System.out.println("e:" + e);
}
}
private void getJdbcDbConnection(String tunnus, String salasana)
{
try
{
//Make the connection object.
con = DriverManager.getConnection(url, tunnus, salasana);
stmt = con.createStatement();
} catch (SQLException e)
{
System.out.println("e:" + e);
}
}
public Vector<Henkilo> haeHenkilot()
{
Vector<Henkilo> henkilot = new Vector<Henkilo>(10, 10);
int henkiloId;
String nimi;
String osoite;
try
{
ResultSet rs = stmt.executeQuery("select HenkiloId, Nimi,Osoite from Henkilo");
System.out.println("Execute ok");
while (rs.next())
{
henkiloId = rs.getInt(1);
nimi = rs.getString(2);
osoite = rs.getString(3);
System.out.println("Kentat ok");
henkilot.addElement(new Henkilo(henkiloId, nimi, osoite));
}
rs.close();
} catch (Exception e)
{
System.out.println("Pieleen meni kirjojen haku\n" + e.toString());
}
return henkilot;
}
public boolean lisaaHenkilo(Henkilo uusihenkilo)
{
boolean ok;
try
{
String insertStr = "INSERT INTO Henkilo VALUES('" + uusihenkilo.getNimi() + "', '" + uusihenkilo.getOsoite() + "');";
System.out.println(insertStr);
stmt.executeUpdate(insertStr);
ResultSet rs = stmt.executeQuery("SELECT @@IDENTITY AS 'Identity'");
if (rs.next())
{
uusihenkilo.setHenkiloId(rs.getInt(1));
System.out.println(uusihenkilo.getHenkiloId());
}
ok = true;
} catch (Exception e)
{
System.out.println("Pieleen meni insert operaatio\n" + e.toString());
ok = false;
}
return ok;
}
public boolean muutaHenkilo(Henkilo henkilo)
{
boolean ok = false;
try
{
String updateStr = "UPDATE Henkilo set Nimi = '" + henkilo.getNimi() + "', osoite = '" + henkilo.getOsoite() + "' WHERE HenkiloId = '" + henkilo.getHenkiloId() + "';";
System.out.println(updateStr);
stmt.executeUpdate(updateStr);
ok = true;
} catch (Exception e)
{
System.out.println("Pieleen meni update operaatio\n" + e.toString());
ok = false;
}
return ok;
}
public boolean poistaHenkilo(int henkiloId)
{
boolean ok;
try
{
String deleteStr = "DELETE FROM Henkilo WHERE henkiloId = " + henkiloId + ";";
System.out.println(deleteStr);
stmt.executeUpdate(deleteStr);
ok = true;
} catch (Exception e)
{
System.out.println("Pieleen meni delete operaatio\n" + e.toString());
ok = false;
}
return ok;
}
public void suljeDbYhteys()
{
try
{
if (stmt != null)
{
stmt.close();
}
if (con != null)
{
con.close();
}
} catch (Exception e)
{
System.out.println(e.toString());
}
}
} |
Partager