void utmatning(){
String sql = "SELECT * FROM `music`.`Artist`;" ;
System.out.println(sql);
ResultSet rs = dbs.query(sql);
String[] array;
while((array =dbs.fetchArray(rs)) != null){
System.out.println(array[1] + " " + array[2]);
}
}
måndag 7 juni 2010
utmatning java - by popular demand
tisdag 30 mars 2010
DatabasServer- java - hur du komunicerar med DB
ppackage musicdb; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author gustafsp */ public class DatabasServer { final static String server = "localhost"; final static int port = 3306; final static String login = "root"; final static String password = "mysql"; final static String databas = "music"; private static Connection con; public static Connection connect() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException ex) { System.out.println("MYSQL driver missing?"); } //Define URL of database server for // database named JunkDB on the localhost // with the default port number 3306. String url = "jdbc:mysql://" + DatabasServer.server + ":" + DatabasServer.port; try { //Get a connection to the database for a // user named auser with the password // drowssap, which is password spelled // backwards. con = DriverManager.getConnection( url, DatabasServer.login, DatabasServer.password); return con; } catch (SQLException ex) { System.out.println("Login Fail: password ok?"); } return null; } public static ResultSet query(String sql) { try { if (con == null) { con = DatabasServer.connect(); } Statement stmt = con.createStatement(); if (stmt.execute(sql)) { return stmt.getResultSet(); } } catch (SQLException ex) { Logger.getLogger(DatabasServer.class.getName()).log(Level.SEVERE, null, ex); } return null; } public static String[] fetchArray(ResultSet result) { try { String[] array = new String[result.getMetaData().getColumnCount()]; if (result.next()) { for (int i = 1; i <= result.getMetaData().getColumnCount(); i++) { array[i - 1] = result.getString(i); } return array; } } catch (SQLException ex) { Logger.getLogger(DatabasServer.class.getName()).log(Level.SEVERE, null, ex); } return null; //om fel eller inget värde } }
tisdag 23 mars 2010
Updatering Sökning, Urval, Statistik
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package musicdb;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author gustafsp
*/
public class DatabasHantering {
// min komentar
DatabasServer dbs = new DatabasServer();
void urval(String urval){
String sql = "SELECT * FROM `music`.`Artist` WHERE name = '" + urval + "';" ;
System.out.println(sql);
ResultSet rs = dbs.query(sql);
String[] array;
while((array =dbs.fetchArray(rs)) != null){
System.out.println(array[1] + " " + array[2]);
}
}
void sok(String sok){
String sql = "SELECT * FROM `music`.`Artist` WHERE name LIKE '%" +
sok +
"%' ;" ;
System.out.println(sql);
ResultSet rs = dbs.query(sql);
String[] array;
while((array =dbs.fetchArray(rs)) != null){
System.out.println(array[0] + " " + array[1] + " " + array[2]);
}
}
void statistik(){
String sql = "SELECT " +
"sum(id) AS sum, " +
"min(id) AS min, " +
"max(id) AS max, " +
"avg(id) AS avg," +
"count(*) AS COUNT " +
"FROM `music`.`Artist` GROUP BY Start ;" ;
System.out.println(sql);
ResultSet rs = dbs.query(sql);
String[] array;
while((array =dbs.fetchArray(rs)) != null){
System.out.println(array[0]
+ " " + array[1]
+ " " + array[2]
+ " " + array[3]
+ " " + array[4]);
}
}
public static void main(String[] args){
DatabasHantering dh = new DatabasHantering();
String s = JOptionPane.showInputDialog("Sök");
System.out.println("Sök");
dh.sok(s);
System.out.println("Urval");
dh.urval(s);
System.out.println("Stat");
dh.statistik();
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package musicdb;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author gustafsp
*/
public class DatabasHantering {
// min komentar
DatabasServer dbs = new DatabasServer();
void urval(String urval){
String sql = "SELECT * FROM `music`.`Artist` WHERE name = '" + urval + "';" ;
System.out.println(sql);
ResultSet rs = dbs.query(sql);
String[] array;
while((array =dbs.fetchArray(rs)) != null){
System.out.println(array[1] + " " + array[2]);
}
}
void sok(String sok){
String sql = "SELECT * FROM `music`.`Artist` WHERE name LIKE '%" +
sok +
"%' ;" ;
System.out.println(sql);
ResultSet rs = dbs.query(sql);
String[] array;
while((array =dbs.fetchArray(rs)) != null){
System.out.println(array[0] + " " + array[1] + " " + array[2]);
}
}
void statistik(){
String sql = "SELECT " +
"sum(id) AS sum, " +
"min(id) AS min, " +
"max(id) AS max, " +
"avg(id) AS avg," +
"count(*) AS COUNT " +
"FROM `music`.`Artist` GROUP BY Start ;" ;
System.out.println(sql);
ResultSet rs = dbs.query(sql);
String[] array;
while((array =dbs.fetchArray(rs)) != null){
System.out.println(array[0]
+ " " + array[1]
+ " " + array[2]
+ " " + array[3]
+ " " + array[4]);
}
}
public static void main(String[] args){
DatabasHantering dh = new DatabasHantering();
String s = JOptionPane.showInputDialog("Sök");
System.out.println("Sök");
dh.sok(s);
System.out.println("Urval");
dh.urval(s);
System.out.println("Stat");
dh.statistik();
}
}
PHP - MYSQL Exempel
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if($link = mysql_connect("localhost:3306", "root", "")) {
echo "inloggad";
$sql = "SELECT * FROM mysql.user LIMIT 0 , 30;";
echo $sql;
$result = mysql_query($sql, $link);
if(!$result) {
echo "noThing just a Fail";
}else {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach($row as $value) {
echo $value. "";
}
echo "<br />";
//print_r($row)
}
}
}
else {
echo "Ej inloggad";
}
?>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if($link = mysql_connect("localhost:3306", "root", "")) {
echo "inloggad";
$sql = "SELECT * FROM mysql.user LIMIT 0 , 30;";
echo $sql;
$result = mysql_query($sql, $link);
if(!$result) {
echo "noThing just a Fail";
}else {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach($row as $value) {
echo $value. "";
}
echo "<br />";
//print_r($row)
}
}
}
else {
echo "Ej inloggad";
}
?>
</body>
</html>
Sök och urvals
package musicdb;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
* @author gustafsp
*/
public class DatabasHantering {
// min komentar
DatabasServer dbs = new DatabasServer();
void urval(String urval){
String sql = "SELECT * FROM `music`.`Artist` WHERE name = '" + urval + "';" ;
System.out.println(sql);
ResultSet rs = dbs.query(sql);
String[] array;
while((array =dbs.fetchArray(rs)) != null){
System.out.println(array[1] + " " + array[2]);
}
}
void sok(String sok){
String sql = "SELECT * FROM `music`.`Artist` WHERE name LIKE '%" +
sok +
"%' ;" ;
System.out.println(sql);
ResultSet rs = dbs.query(sql);
String[] array;
while((array =dbs.fetchArray(rs)) != null){
System.out.println(array[1] + " " + array[2]);
}
}
public static void main(String[] args){
DatabasHantering dh = new DatabasHantering();
String s = JOptionPane.showInputDialog("Sök");
System.out.println("Sök");
dh.sok(s);
System.out.println("Urval");
dh.urval(s);
}
}
Prenumerera på:
Inlägg (Atom)